C# 我想使按钮文本在单击时更改后保持不变

C# 我想使按钮文本在单击时更改后保持不变,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我在做我的项目,我有一个网格视图,其中有一个状态字段,其中有一个按钮。 当用户单击该按钮时,该按钮的文本将从挂起更改为确认。 问题是,当我关闭浏览器并通过visual studio再次加载.aspx页面时,按钮的文本再次设置为挂起,这是我不希望看到的。 任何人都可以告诉我如何保持按钮文本,以确认只有一次按钮被点击,而且当我点击第一行状态字段按钮时,所有其余状态字段都会更新为数据库中的确认状态。 这是我的代码:(前端代码) (后端代码): 受保护的无效页面加载(对象发送方,事件参数e) { 如

我在做我的项目,我有一个网格视图,其中有一个状态字段,其中有一个按钮。 当用户单击该按钮时,该按钮的文本将从挂起更改为确认。 问题是,当我关闭浏览器并通过visual studio再次加载.aspx页面时,按钮的文本再次设置为挂起,这是我不希望看到的。 任何人都可以告诉我如何保持按钮文本,以确认只有一次按钮被点击,而且当我点击第一行状态字段按钮时,所有其余状态字段都会更新为数据库中的确认状态。 这是我的代码:(前端代码)


(后端代码):

受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
List lst=新列表(){“asd”,“xxx”};
GridView1.DataSource=lst;
这个.BindGrid();
}
}
受保护的无效btnpreviewwebsite\u Click1(对象发送者,事件参数e)
{
重定向(“~/index.aspx”);
}
受保护的无效btnlogout\u单击(对象发送者,事件参数e)
{
会话。放弃();
Session.Clear();
重定向(“~/Admin Panel/LoginForm.aspx”);
}
私有void BindGrid()
{
使用(SqlConnection con=new SqlConnection(@“数据源=。\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;集成安全性=True;用户实例=True)))
{
使用(SqlCommand cmd=newsqlcommand(“从查询中选择packagename、姓名、性别、手机、电子邮件、noofdays、儿童、成人”))
{
使用(SqlDataAdapter sda=newsqldataadapter())
{
cmd.Connection=con;
sda.SelectCommand=cmd;
使用(DataTable dt=newdatatable())
{
sda.填充(dt);
GridView1.DataSource=dt;
GridView1.DataBind();
}
}
}
}
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
if(e.CommandName==“MYCOMMAND”)
{
SqlConnection con=new SqlConnection(@“数据源=。\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;集成安全性=True;用户实例=True”);
字符串sql;
Button Button1=(Button)e.CommandSource;
如果(按钮1!=null)
按钮1.Text=“确认”;
sql=“更新查询集状态字段=”+Button1.Text+”;
SqlCommand comm=新的SqlCommand(sql,con);
con.Open();
comm.ExecuteNonQuery();
}
}

您可以使用asp.net中的绑定功能,以便从DB结果中获取按钮文本。如下图所示

在aspx页面中

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"
    Font-Bold="True" Font-Size="Small" GridLines="None">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="enquiryid" HeaderText="Enquiryid" />
        <asp:BoundField ItemStyle-Width="150px" DataField="packagename" HeaderText="Package" />
        <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="gender" HeaderText="Gender" />
        <asp:BoundField ItemStyle-Width="150px" DataField="mobileno" HeaderText="Mobile No" />
        <asp:BoundField ItemStyle-Width="150px" DataField="email" HeaderText="Email" />
        <asp:BoundField ItemStyle-Width="150px" DataField="noofdays" HeaderText="No. of Days" />
        <asp:BoundField ItemStyle-Width="150px" DataField="child" HeaderText="No. of Children" />
        <asp:BoundField ItemStyle-Width="150px" DataField="adults" HeaderText="No of Adults" />
        <asp:TemplateField HeaderText="Status Field">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND"
                    Text='<%# Bind("statusfield")%>' BorderStyle="None" Font-Bold="True" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

谢谢,即使在我重新加载页面时,按钮值仍保留在确认文本上,但整个表列(即状态字段)将更改为确认。您能告诉我如何仅更新一行吗@Hemant Dyou需要根据主键在update语句中添加where条件。我知道我需要在update查询中使用where条件,但我需要在where条件中使用哪个控件,您可以告诉我@Hemant数据表“查询”的主键是什么?
 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<string> lst = new List<string>() { "asd", "xxx" };
        GridView1.DataSource = lst;
        this.BindGrid();
    }


}
protected void btnpreviewwebsite_Click1(object sender, EventArgs e)
{
    Response.Redirect("~/index.aspx");
}
protected void btnlogout_Click(object sender, EventArgs e)
{
    Session.Abandon();
    Session.Clear();
    Response.Redirect("~/Admin Panel/LoginForm.aspx");

}
private void BindGrid()
{

    using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True"))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT packagename,name, gender,mobileno,email,noofdays,child,adults FROM enquiry"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{



    if (e.CommandName == "MYCOMMAND")
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True");
        string sql;
        Button Button1 = (Button)e.CommandSource;
        if (Button1 != null)
            Button1.Text = "Confirm";
        sql = "update enquiry set statusfield='" + Button1.Text + "'";

        SqlCommand comm = new SqlCommand(sql, con);

        con.Open();
        comm.ExecuteNonQuery();

    }
}
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"
    Font-Bold="True" Font-Size="Small" GridLines="None">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="enquiryid" HeaderText="Enquiryid" />
        <asp:BoundField ItemStyle-Width="150px" DataField="packagename" HeaderText="Package" />
        <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="gender" HeaderText="Gender" />
        <asp:BoundField ItemStyle-Width="150px" DataField="mobileno" HeaderText="Mobile No" />
        <asp:BoundField ItemStyle-Width="150px" DataField="email" HeaderText="Email" />
        <asp:BoundField ItemStyle-Width="150px" DataField="noofdays" HeaderText="No. of Days" />
        <asp:BoundField ItemStyle-Width="150px" DataField="child" HeaderText="No. of Children" />
        <asp:BoundField ItemStyle-Width="150px" DataField="adults" HeaderText="No of Adults" />
        <asp:TemplateField HeaderText="Status Field">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND"
                    Text='<%# Bind("statusfield")%>' BorderStyle="None" Font-Bold="True" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 private void BindGrid()
{

   using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True"))
    {

    using (SqlCommand cmd = new SqlCommand("SELECT enquiryid,packagename,name, gender,mobileno,email,noofdays,child,adults,statusfield FROM enquiry"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}