如何在单击编辑按钮的gridview中获取文本框值?

时间:2014-06-30 06:18:27

标签: 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;

但是在调试时我总是在那里得到空引用。

这是我的行更新代码,它运行正常

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);
    }

3 个答案:

答案 0 :(得分:1)

当我尝试从编辑按钮上的文本框中获取数据时,我会这样做:

<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");
    }
}

答案 1 :(得分:0)

on row databound事件跟踪行状态并进行必要的调用以获取FindControl()对象上调用GridViewRow所需控件的引用。

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";
    }
}

代码来自here

答案 2 :(得分:0)

GridViewEventArgs具有正在编辑的行的索引。看起来你没有使用事件args中的索引。试试这个:

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
}