I have to Update Panels in my application and i want to make everything work with the code not with the GUI. So when i click insert button it should insert the data from the textboxes to the database and in UpdatePanel2 i want GridView to be automaticly updated. It inserts the data but does not update the GridView , so here is the C# code that i wrote for it
1protected void Page_Load(object sender, EventArgs e)2 {3if (!Page.IsPostBack)4 {5 dataGetir();6 }7else8 {9 dataGetir();10 }1112 }1314protected void dataGetir()15 {16 SqlConnection conn =new SqlConnection("Server=localhost;Database=Calisma;Trusted_Connection=True;");17 SqlCommand cmd =new SqlCommand("Select * From Calisanlar", conn);18 SqlDataAdapter da =new SqlDataAdapter(cmd);1920 DataSet ds =new DataSet();21 da.Fill(ds);22 GridView1.DataSource = ds;23 GridView1.DataBind();24 }25protected void btnInsert_Click(object sender, EventArgs e)26 {27 SqlConnection conn =new SqlConnection("Server=localhost;Database=Calisma;Trusted_Connection=True;");28 SqlCommand cmd =new SqlCommand("insert into Calisanlar(FirstName, LastName) Values(@dotnet.itags.org.First, @dotnet.itags.org.Last)", conn);29 SqlParameter prmFirst =new SqlParameter("@dotnet.itags.org.First", SqlDbType.NVarChar, 50);30 prmFirst.Value = txtIsim.Text;31 SqlParameter prmLast =new SqlParameter("@dotnet.itags.org.Last", SqlDbType.NVarChar, 50);32 prmLast.Value = txtSoyad.Text;3334 cmd.Parameters.Add(prmFirst);35 cmd.Parameters.Add(prmLast);3637 conn.Open();38 cmd.ExecuteNonQuery();39 txtIsim.Text = String.Empty;40 txtSoyad.Text = String.Empty;41 conn.Close();4243 }44protected void btnCancel_Click(object sender, EventArgs e)45 {46 txtIsim.Text = String.Empty;47 txtSoyad.Text = String.Empty;48 }
And here is the source code that i wrote for the panels
1<form id="form1" runat="server">2 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />3 <div>4 <asp:UpdatePanel ID="UpdatePanel1" runat="server" >5 <ContentTemplate>6 <table style="width: 200px; border-width: 0px; background-color:Blue;">7 <tr>8 <td style="width: 4px">9 <asp:Label ID="Label1" runat="server" Text="?sim"></asp:Label></td>10 <td>11 <asp:TextBox ID="txtIsim" runat="server"></asp:TextBox></td>12 </tr>13 <tr>14 <td style="width: 4px">15 <asp:Label ID="Label2" runat="server" Text="Soyad"></asp:Label></td>16 <td>17 <asp:TextBox ID="txtSoyad" runat="server"></asp:TextBox></td>18 </tr>19 <tr>20 <td style="width: 4px">2122 </td>23 <td>24 <table style="width: 150px; border-width: 0; background-color: Blue;">25 <tr>26 <td style="width: 33px; height: 26px">27 <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" /></td>28 <td style="height: 26px">29 <asp:Button ID="btnICancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></td>30 </tr>31 </table>32 </td>33 </tr>34 </table>35 </ContentTemplate>36 </asp:UpdatePanel>37 <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">38 <ContentTemplate>39 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">40 <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />41 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />42 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />43 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />44 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />45 <AlternatingRowStyle BackColor="White" />46 </asp:GridView>47 </ContentTemplate>48 <Triggers>49 <asp:AsyncPostBackTrigger ControlID="btnEkle" EventName="Click" />50 </Triggers>51 </asp:UpdatePanel>5253 </div>54 </form>So i am so confused about Ajax, thanks for your helps
You need to open the connection to your database for it to send the query. One way to check this is by using the Sql Profiler inside of the SQL Server Management Studio under the Tools menu. That could help you diagnose in the future a problem like this.
To fix modify you code like so:
16 SqlConnection conn =new SqlConnection("Server=localhost;Database=Calisma;Trusted_Connection=True;");
17 SqlCommand cmd =new SqlCommand("Select * From Calisanlar", conn);
18 SqlDataAdapter da =new SqlDataAdapter(cmd);
19
20 DataSet ds =new DataSet();
21 conn.Open();
22 da.Fill(ds);
23 conn.Close();
24 GridView1.DataSource = ds;
25 GridView1.DataBind();
Also in your Page_Load routine you shouldn't really call the binding sub routine in both conditions. You really don't need the IF statement because it runs everytime no matter what.
It should look like this:
3 if (!Page.IsPostBack)
4 {
5 dataGetir();
6 }
7 else
8 {
9 //Nothing here
10 }
Let me know how that works.
JoeWeb
I modified the lines like you said. But it didn't work i mean it inserts the data into database but it doesn't refresh the GridView. When i refresh the whole page then the GridView refreshes too. I guess there is something wrong with the source code. Because normally the code that i wrote insterts the data and selects the data no problem. The problem is in the Update Panel i guess?
Mehmet Serif
You are doing an AsyncTrigger... with this control btnEkle
I don't see you referencing that control anywhere in the frontend code. You need to put that button inside the updatepanel enclosing the gridview for the updatepanel to be triggered for an update. Otherwise just remove the asynctrigger and it will update the panel nonetheless.
JoeWeb
btnEkle is already referenced in UpdatePanel2 as an AsyncTrigger control with its Click event..and btnEkle button is in UpdatePanel1 and should trigger the UpdatePanel2..if you say it's not referenced to anywhere then how can i reference it?
Thanks
I think what he's saying is that in the code you pasted in this thread, there's no button called btnEkle.
oh yes you are right but in its original code there is no btnInsert instead there is a btnEkle so no problem about the buttons..
Would you mind pasting the actual code you're using (since this apparently wasn't it)?
My best guess without being able to see the code is that you need to call DataBind() on your DataGrid in your click event handler.
ughnn i forgot to call dataGetir() function in Click event of btnEkle button. Thanks for the help..
No comments:
Post a Comment