Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过单击编辑按钮在gridview中获取文本框值?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何通过单击编辑按钮在gridview中获取文本框值?

C# 如何通过单击编辑按钮在gridview中获取文本框值?,c#,asp.net,gridview,C#,Asp.net,Gridview,我是asp.net的新手,我已经尝试了一段时间以上的问题,但没有成功。这是我在roweditevent中的代码 GridView1.EditIndex = e.NewEditIndex; int newindex = e.NewEditIndex; TextBox NAME = GridView1.Rows[newindex].FindControl("txtboxname") as TextBox; string gridupdat = NAME.Text; 但在调试时,我总是得到空

我是asp.net的新手,我已经尝试了一段时间以上的问题,但没有成功。这是我在roweditevent中的代码

 GridView1.EditIndex = e.NewEditIndex;
 int newindex = e.NewEditIndex;
 TextBox NAME = GridView1.Rows[newindex].FindControl("txtboxname") as TextBox;
 string gridupdat = NAME.Text;
但在调试时,我总是得到空引用

这是我的行更新代码,工作正常

Label ID = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
    TextBox NAME = GridView1.Rows[e.RowIndex].FindControl("txtboxname") as TextBox;
    DropDownList STATUS = GridView1.Rows[e.RowIndex].FindControl("dropdownstatus") as DropDownList;
    string string1 = NAME.Text;
    if (fetchmail(string1, labelgrid999) == true)
    {
        string updatquery = string.Format("UPDATE Compliance_Tracker.dbo.verificationMaster SET NAME='{0}',STATUS='{1}' WHERE ID = {2}", NAME.Text, STATUS.Text, Convert.ToInt32(ID.Text));
        string dupquery = "select COUNT(*) from Compliance_Tracker.dbo.verificationMaster where Compliance_Tracker.dbo.verificationMaster.NAME = '" + NAME.Text + "';";
        if (obj4.isDuplicate(dupquery) == false )
        {
            GridView1.EditIndex = -1;
            string populatequery = updatquery + ";select NAME,(case when STATUS='1' then 'Active' when STATUS='0'then 'Inactive' ELSE 'UNKNOWN' END)as STATUS,ID from Compliance_Tracker.dbo.verificationMaster;";
            obj4.BindGridData(populatequery, GridView1);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Username already exists" + "');", true);
        }
    }
    else
    {
        string myStringVariable = "Please enter valid username";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
    }

在行数据绑定事件跟踪行状态并进行必要的调用,以获取对
GridViewRow
对象调用
FindControl()
所需控件的引用

protected void gvMaint_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowState == DataControlRowState.Edit)
    {
        TextBox txtFreqMiles = (TextBox)e.Row.FindControl("txtFreqMiles");

        // At this point, you can change the value as normal
        txtFreqMiles.Text = "some new text";
    }
}

取自GridViewEventArgs的代码具有正在编辑的行的索引。看起来您没有使用事件参数中的索引。试试这个:

protected void edit(object sender, GridViewEditEventArgs e)         
{

    string gridupdat = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
    // where Cells[0] = the index of your "txtboxname" column
}

这是我在编辑按钮单击时尝试从文本框中获取数据时所做的操作:

<asp:TemplateField HeaderText="Edit">
   <ItemTemplate>
      <asp:Button ID="BtnEdit" CausesValidation="false" runat="server" Text="Update" CommandName="updateData" CommandArgument='<%# Eval("ID") %>' />
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
   <ItemTemplate>
       <asp:TextBox ID="TxtPriceEdit" runat="server" Text='<%# Eval("Product_Price") %>' Width="120px"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void GrdDataEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "updateData")
    {
        // This will give you the ID of the record you are passing in the CommandArgument='<%# Eval("ID") %>'  
        int i = Convert.ToInt32(e.CommandArgument); 

        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        TextBox price = (TextBox)row.FindControl("TxtPriceEdit");
    }
}
受保护的void GrdDataEdit_行命令(对象发送方,GridViewCommandEventArgs e)
{
如果(e.CommandName==“updateData”)
{
//这将为您提供在CommandArgument=''中传递的记录的ID
inti=Convert.ToInt32(如CommandArgument);
GridViewRow行=(GridViewRow)((按钮)e.CommandSource.NamingContainer);
TextBox price=(TextBox)row.FindControl(“txtpricedit”);
}
}

我猜找不到您的txtboxname。您在哪一行获得异常?但我在行更新事件中使用了相同的代码,并且它工作正常,因此未找到
Name
。你确定txtboxname是正确的吗?是的,我应该把行更新代码给你吗?如果有人能找到问题所在,请发布…但是,正如你在我的代码中看到的,我使用的是eventargs中的索引。你使用的是e.RowIndex而不是e.NewEditIndex。问题在编辑代码中,在这一部分,我正在使用e。neweditindex@SQL.NETWarrior:第一行。因此,我们无法在GridView1_RowEditing(对象发送者,GridViewEditEventArgs e)事件中获取值。可能没有其他方法,但我就是这样做的,来完成我的工作。没关系,一切都是为了帮助,声誉不重要。我很高兴为您提供帮助。当您单击模板字段列上的“编辑”按钮(该列具有文本框列)并且网格正在编辑模式下进行数据绑定时,将运行此代码。换言之,场景是,如果要用默认值填充文本框,可以执行以下操作。在我回答问题之前,这与您最近声明的目的完全不同。好的,但我的问题是在gridview文本框中获取我获取的值now@user3789152好啊没问题。只要确保下次发布问题时,提供准确的细节,以帮助重现问题或帮助其他人更快地理解。欢迎来到SO,祝您节目愉快。:)