如何在gridview中获取动态创建的文本框的更改值?

问题描述:

我正在尝试根据主网格视图点击编辑我的详细网格视图。我正在动态添加文本框并进行编辑。当我点击更新按钮时它会给我旧的值,而我需要新的。这就是我的意思到目前为止。请建议。



I am trying to edit my detailed gridview based on master gridview click.I am adding a textbox dynamically and editing it.when i click on the update button it gives me the old value whereas i require the new.This is what i have done so far.Please suggest.

protected void btnedit_Click(object sender, EventArgs e)
  {


      int index = GridView1.EditIndex;

      Button btn = (Button)sender;


      GridViewRow gvr = (GridViewRow)btn.NamingContainer;

      int getindex = gvr.RowIndex;

      //string headertext = this.GridView1.Columns[1].HeaderText;
      string id = GridView1.Rows[getindex].Cells[4].Text;

      DataTable dtfrweekend = new DataTable();
      connfordata.Open();
      SqlCommand cmdfrgetweekend = new SqlCommand("select s001_Weekending as Weekend from dbo.s001_Timesheets where s001_TimesheetsID ='" +id+ "' ", connfordata);
      SqlDataAdapter dafrweekend = new SqlDataAdapter(cmdfrgetweekend);
      dafrweekend.Fill(dtfrweekend);
      connfordata.Close();

          BoundField bf = new BoundField();
          bf.DataField = "Monday";
          txtmonday.Text = GridView1.Rows[getindex].Cells[3].Text;
          GridView1.Rows[getindex].Cells[3].Controls.Add(txtmonday);

  }
















protected void btnupdate_Click(object sender, EventArgs e)
{

    //string get = txtmonday.Text;
    //String sValue = Request.Form["txtmonday"];


    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    int getindex = gvr.RowIndex;

    TextBox tb = GridView1.FindControl("txtmonday") as TextBox;



}





是否可以获得在另一个函数中从动态创建的文本框中更改了值?



Is it possible to get the changed value from dynamically created textbox in another function?

动态控制在回发原因之后并不简单。由于Web本质上是断开连接,因此它会创建页面的一个对象,然后使用该对象创建所需的HTML并发送回呈现它的浏览器。因此,尽管有多次回发,ViewState仍然会在页面上进行回发和管理控制后进行处理。意味着没有添加到ViewState的动态控件?是的。



但是HTML页面上任何具有输入类型的东西都能够将数据发送到服务器。意味着您的文本框是输入类型并发布数据所以如何获取该值。看下面



如果我在页面加载页面上添加一些控件如

Dynamic Controls not avaialbe after postback reason is simple. As web is disconnect in nature it create one object of your page then create required HTML using that and send back to browser that renders it. So ViewState comes into picture to take care after post back and mange control on page despite multiple postbacks. Means dynamic controls not added to ViewState? Yes.

However any thing on HTML page that has type of input has ability to sent data to server. Means your text box is input type and does post data So how to get that value . Look below

if I add some control to page on page load like
if (!IsPostBack)
            {
                TextBox t = new TextBox();
                t.ID = "txt1";
                pnl1.Controls.Add(t);

            }





然后我可以在回发后获得价值



then I can get values after postback as

if (Request["txt1"] != null)
            {
                value = Request["txt1"];

            }





这只是概念不是你问题的完全解决方案..我想你得到的逻辑。



This is just concept not exact solution to your problem .. I guess you get the logic.


你好,





将你的textBox放在Item模板中并编写硬编码的AutoPostBack =Trueontextchanged =txtBox_TextChanged在aspx页面上的那个textBox之后



代码背后创建该文本框的事件喜欢 -





Hello ,


put your textBox in Item template and write hard coded "AutoPostBack="True" ontextchanged="txtBox_TextChanged" " on that textBox on aspx page after that

in code behind create event of that text box Like -


protected void txtBox_TextChanged(object sender, EventArgs e)
    {  }









它将在gridview中用于textchange事件。









It will work in gridview for textchange event.


<asp:TemplateField HeaderText ="Sch.End Date" >
                    <ItemTemplate >
                        <asp:TextBox ID="txtBox" runat="server" ontextchanged="txtBox_TextChanged"></asp:TextBox>
                    </ItemTemplate>
              </asp:TemplateField>