且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在网格视图中添加新行时如何保留上次输入的行值?

更新时间:2023-12-03 17:14:52

我通过在网格中添加OnRowDataBound事件来更新我的解决方案:



I have updated my solution here by adding OnRowDataBound event in grid:

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false" OnRowDataBound="GridView_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"  /> 
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"  /> 
            </ItemTemplate>
        </asp:TemplateField>
        
    </Columns>
</asp:GridView>





添加javascript使用此:





to add javascript use this:

<script type="text/javascript">
         function Handlejavascript(text1, text2) {
             document.getElementById(text2).value
             document.getElementById(text1).value = document.getElementById(text2).value;
         }
    </script>





在.cs文件中调用此使用代码



to call this use code in .cs file

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox TextBox1 = e.Row.FindControl("TextBox1") as TextBox;
                TextBox TextBox2 = e.Row.FindControl("TextBox2") as TextBox;
                //TextBox TextBox3 = e.Row.FindControl("TextBox2") as TextBox;

                TextBox2.Attributes.Add("onchange", "Handlejavascript('" + TextBox1.ClientID + "', '" + TextBox2.ClientID + "');");
                
            }
        }

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
                dt.Rows.Add(1, "VCJ", "United States");
                dt.Rows.Add(2, "Your name", "India");
                dt.Rows.Add(3, "My Name", "France");
                dt.Rows.Add(4, "Gopinath", "India");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }





希望这对您有所帮助:如果您想显示值,请将该值作为参数传递在java脚本函数中



hope this might help you : if you want to show value pass that value as parameter in java script function