Posted: 7/4/2010
The logic in a nutshell is I have a sqlDataSource that gets some data. Then a repeater control uses that data source to display a list on the page. Then the user can add to this list by typing in a textfield and clicking a button. Everything displays fine on the initial page load. The user types in some data to add to the list and clicks a button. To avoid a complete page post back I use a ajax event to capture the click and insert the user's data into the db. Then I call Select on the sqlDataSource to grab the data from the db again. Then I call DataBind on the repeater to refresh the list and display the data agian. This is where the error occurs. I beleive the error occurs because on the initial page load the sqlDataSource returns (lets say) 2 records and the repeater creates 2 controls from the item template to display the 2 records. Then the user adds some data and clicks the button. My code inserts the data into the db and then the Select call on the sqlDataSource retuns (now) 3 records but the DataBind call on the repeater binds the first two records and then chokes and pukes on the third because there is only 2 controls in the repeater. I need the repeater to rerender and create (in this example) 3 controls form its item template.
What I'm I missing here or is this even the reason I get the error.
Hello Tyler and Welcome to the CodeASP.NET Community.
I think you are doing something wrong in your code. Probably showing the necessary parts of your code will be crucial for us to give you good advices and possible solutions.
However, I have created one simple solution which does what you (as I think) you need.
ASPX code
<form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ADD" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> <%# Eval("Item") %> <br /> <!-- replace your ItemTemplate aspx code as appropriate --> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySampleDBConnectionString %>" SelectCommand="SELECT [Item] FROM [Item]"></asp:SqlDataSource> </ContentTemplate> </asp:UpdatePanel> </form>
If you see, I'm using ScriptManager and UpdatePanel. Have one Button ADD and TextBox to add it in the Repeater. In the Repeater, I have one <%# Eval('Item') %> which I get from the SqlDataSource's select command retrieved from database. You should replace your own commands, Repeater ItemTemplate ASPX code and the Connection String in the SqlDataSource.
Then, In code behind everything you need to do is the following.
On Button1_Click method
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MySampleDBConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("insert into Item values ('" + TextBox1.Text + "')", con); using (con) { con.Open(); cmd.ExecuteNonQuery(); } //replace above code with your code for INSERTING INTO DATABASE TABLE //then just call the Repeater1.DataBind();, nothing else. Repeater1.DataBind();
Posted: 7/5/2010
<!-- Scripts -->
<script runat="server" type="text/VB">
Protected Function CompleteAdd(ByVal sender As Object, ByVal e As AjaxEventArgs)
lblAddDateCode.Text = ""
Dim sds As New SqlDataSource
sds.ConnectionString = "Data Source=Cognos;Initial Catalog=QEITWEB;Integrated Security=True"
sds.UpdateCommandType = SqlDataSourceCommandType.Text
sds.UpdateCommand = "EXECUTE [dbo].[sp_UPDATE_RMR_STAGING_TABLES] '" & rmrNumber & "', '" & e.ExtraParams("rmrTable") & "', '" & e.ExtraParams("rmrColumn") & "', '" & e.ExtraParams("value") & "', '{none}';"
sds.Update()
sdsGetRmrDateCodes.Select(DataSourceSelectArguments.Empty)
repDateCodes.DataBind()
udpDateCodes.Update()
End Function
</script>
<!-- Date Codes Panel -->
<ext:Panel ID="pnlDateCodes" runat="server" Title="Date Codes" Cls="sectionPanel" Frame="True" Icon="Calendar">
<Body>
<asp:UpdatePanel ID="udpDateCodes" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<!-- Date Codes Table -->
<table cellpadding="0" cellspacing="0" class="panelTable">
<!-- Date Codes Row -->
<tr class="dataRows">
<td class="labelFields">
<!-- Date Codes Label -->
<asp:Label ID="lblDateCodes" runat="server" Text="Date Codes:  " />
</td>
<td class="dataFields">
<table>
<!-- Date Codes Repeater -->
<asp:Repeater ID="repDateCodes" runat="server" DataSourceID="sdsGetRmrDateCodes" >
<ItemTemplate>
<!-- Date Codes Control -->
<ext:Label
ID="lblDateCodesData"
runat="server"
Cls="editable"
Icon="NoteEdit"
IconAlign="Left"
OverCls="editable-over"
EmptyText="Click here to edit"
Text='<%# Eval("RMB_BUILD_DATE") %>'>
<Editor>
<ext:Editor ID="edtDateCode" runat="server">
<AjaxEvents>
<StartEdit OnEvent="StartEdit">
<ExtraParams>
<ext:Parameter Name="oldValue" Value="value" Mode="Raw"/>
</ExtraParams>
</StartEdit>
<Complete OnEvent="CompleteEdit">
<ext:Parameter Name="rmrTable" Value="RMR_BUILD_DATE_STG" Mode="Value" />
<ext:Parameter Name="rmrColumn" Value="RMB_BUILD_DATE" Mode="Value" />
<ext:Parameter Name="value" Value="value" Mode="Raw" />
</Complete>
</AjaxEvents>
<Alignment ElementAnchor="Left" TargetAnchor="Left" />
<Field>
<ext:TextField
ID="txfDateCode"
Cls="x-form-field-editor"
AllowBlank="false"
SelectOnFocus="true"
Width="200px"
MaxLength="20" />
</Field>
</ext:Editor>
</Editor>
</ext:Label>
<td>
<!-- Delete Command Control -->
<ext:Button ID="Button1" runat="server" Icon="Cross" Flat="True" />
</tr>
</ItemTemplate>
</asp:Repeater>
<!-- Add Command Control -->
<tr>
ID="lblAddDateCode"
Icon="ControlAddBlue"
EmptyText="Add Another">
<ext:Editor ID="edtAddDateCode" runat="server">
<Complete OnEvent="CompleteAdd">
ID="TextField2"
MaxLength="20"/>
</table>
<!-- Date Codes Data Source -->
<asp:SqlDataSource ID="sdsGetRmrDateCodes" runat="server"
ConnectionString="<%$ ConnectionStrings:QEITWEBConnectionString %>"
SelectCommandType="StoredProcedure"
SelectCommand="sp_GET_RMR_DATA">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="20100405-01" Name="RMR_NUMBER"
QueryStringField="rmrNumber" Type="String" />
<asp:Parameter DefaultValue="DATE CODES" Name="DATA_TYPE" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</Body>
</ext:Panel>
Well, I don't have the ExtJS right on this machine, but will test it later or tomorrow, surely.
Meanwhile, instead of using the SqlDataSource to execute the update command, try doing that using standard SqlCommand as I did that in my previous post.
Use the SqlDataSource just to select the data for the Repeater.
There is possibility that the ScriptManagers of both AJAX frameworks might make conflict, but let met first test that then will reply shortly with my suggestion.
Also, I see you are calling the CompleteAdd method on Complete event of your Ext Editor. You should take a look around there. I'm not sure if it is a suitable event for calling that method.
Again, I will reply once I try the code.
Regards,Hajan