gpt4 book ai didi

c# - 使用 asp.net 中的更新面板单击按钮更新 gridview

转载 作者:行者123 更新时间:2023-11-30 17:07:54 26 4
gpt4 key购买 nike

我有一个 gridview 定义为

 <asp:UpdatePanel ID="up2" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" DataSourceID="employee" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" DataKeyNames="id" AllowPaging="true" AllowSorting="true" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="employee" runat="server" ConnectionString="values go here" SelectCommand="select * from employee" UpdateCommand="update employee set name=@name, company=@company, salary=@salary where id=@id" DeleteCommand="delete from employee where id=@id"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>

我需要在每次单击“提交”按钮时更新它。提交按钮定义为

 protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
SqlConnection xconn = new SqlConnection();
xconn.ConnectionString = @"values go here";
xconn.Open();
string str = "insert into employee(name, company, salary) values(@name, @company, @salary)";
SqlCommand ycmd = new SqlCommand(str, xconn);
ycmd.Parameters.Add("@name", txtname.Text);
ycmd.Parameters.Add("@salary", txtsalary.Text);
ycmd.Parameters.Add("@company", txtcompany.Text);
ycmd.ExecuteNonQuery();
lblresult.Text = "Record successfully inserted";
}
catch (SqlException e1)
{
lblresult.Text = "<span style='color:red;'>" + e1.Message + "<br />" + e1.StackTrace + "</span>";
}
catch (Exception e2)
{
lblresult.Text = "<span style='color:red;'>" + e2.Message + "<br />" + e2.StackTrace + "</span>";
}
finally
{
txtname.Text = "";
txtcompany.Text = "";
txtsalary.Text = "";
}
}

但是,网格不会在 btnSubmit_Click 上更新

有什么解决方案?

最佳答案

你需要

GridView1.DataBind();

强制网格重新查询数据。

关于c# - 使用 asp.net 中的更新面板单击按钮更新 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14186162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com