2014-11-21 94 views
-2

我有10行的Gridview,每行都有2列,即Textbox和Checkbox。将GridView的单行TextBox值传递给另一行文本框的值

我必须做的是必须输入文本框值为1000和我点击第一行的复选框然后值必须去第二行的文本框,我点击第二行的复选框,然后值必须去第三行文本框等。

我想这一点,

protected void txtintroId_TextChanged(object sender, EventArgs e) 
    { 
     TextBox txt = (TextBox)sender; 
     GridViewRow grid = ((GridViewRow)txt.Parent.Parent.Parent); 
     TextBox txtIntro = (TextBox)txt.FindControl("txtintroId"); 
    } 

在这里我得到的第1行的价值,但如何传递给第2行。

协助我

+0

行是固定的? 10? – yogi970 2014-11-21 11:36:32

+0

它可能会更改为50 – 2014-11-21 11:39:39

+0

您尝试过什么? – yogi970 2014-11-21 11:44:31

回答

0

这里充满工作的代码(按照您的要求)

  1. 像这样创建

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("txtcolumn") %>'></asp:TextBox> </ItemTemplate>
</asp:TemplateField> <asp:TemplateField>
<ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

一个gridview
  • 在C#代码中添加一个函数#

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { int rowNo=0; foreach (GridViewRow GV1 in GridView1.Rows) { rowNo++;// if (rowNo < GridView1.Rows.Count) //Checking that is this last row or not {
    //if no CheckBox chkbx= (CheckBox)GV1.FindControl("CheckBox1"); TextBox curenttxtbx = (TextBox)GV1.FindControl("TextBox1"); if (chkbx.Checked == true)
    `{

     int nextIndex = GV1.RowIndex + 1;//Next row 
         TextBox txtbx = (TextBox)GridView1.Rows[nextIndex].FindControl("TextBox1"); 
         txtbx.Text = curenttxtbx.Text; 
    
        } 
    
    } 
        //if yes 
    else 
    { 
        //For last row 
        //There is no next row 
    } 
    

    } }

  • ,我用=新数据表()这个数据表作为数据源

    DataTable的表; table.Columns.Add(“txtcolumn”,typeof(string));

    table.Rows.Add("1"); 
    table.Rows.Add("32"); 
    table.Rows.Add("32"); 
    table.Rows.Add("32"); 
    table.Rows.Add("3"); 
    table.Rows.Add("32"); 
    table.Rows.Add("32"); 
    
  • 我测试此代码,它正在按您的要求。 (对于这种糟糕的格式,SORRY,我会稍后再做,或请一些机构改正:))

    +0

    感谢您的努力。我刚刚通过使用Session做了。那个怎么样 ? :)) – 2014-11-21 12:31:18

    +0

    这种方法非常干净,与会话相比,会占用更少的内存。通过使用会话不是一个好主意!你会浪费服务器的内存。 – yogi970 2014-11-21 12:33:18

    +0

    你可以检查代码,如果你喜欢那么它是好的,如果不是那么也OK :) – yogi970 2014-11-21 12:34:15

    相关问题