在网格视图中添加新行时如何保留上次输入的行值? [英] How to keep last entered row values when adding new row in grid view?

查看:67
本文介绍了在网格视图中添加新行时如何保留上次输入的行值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的问题发布在以下链接中,请尽快给我一个建议。等待好的回复。



http://stackoverflow.com/questions/26257112/how-to-keep-row-values-in-gridview-after-adding-new-row [< a href =http://stackoverflow.com/questions/26257112/how-to-keep-row-values-in-gridview-after-adding-new-rowtarget =_ blanktitle =New Window> ^ ]

推荐答案

我通过在网格中添加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


这篇关于在网格视图中添加新行时如何保留上次输入的行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆