使用JavaScript/Jquery获取网格视图隐藏字段值 [英] get grid view hidden field value using JavaScript/Jquery

查看:76
本文介绍了使用JavaScript/Jquery获取网格视图隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的英语,我在我的项目中遇到了一个挑战,即,每当我开始使用JavaScript或Jquery在网格视图中访问隐藏字段值时,我都会遇到类似hidden field doesn't exist in current context的编译错误,因此我该如何访问隐藏字段字段值?

Forgive my English, I had one challenge in my project I,e whenever I started to access hidden field value which in grid view using JavaScript or Jquery, I'm getting compilation error like hidden field doesn't exist in current context so how can I access hidden field value?

SelectPatientInfo.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">
    <script type="text/javascript">
        function DispValue(sender, e) {
            var id = e.get_value();
            document.getElementById("<%=PatientRefferalId.ClientID%>").value=id; //getting error here 
        }
    </script>

    <div align="left" style="float: left; margin-left: 5px;">
        <asp:GridView ID="gvPatient" runat="server" AutoGenerateColumns="false" EnableViewState="true">
            <Columns>
                <asp:TemplateField HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="12px" HeaderStyle-Height="20px">
                    <HeaderTemplate>&nbsp;Patient Name&nbsp;</HeaderTemplate>
                    <ItemTemplate>
                        <asp:HiddenField ID="PatientRefferalId" runat="server" Value="0" />
                        <PUC:PatientUserControl ID="pucPatient1" runat="server" OnClientSelect="DispValue" PTStatusShow="0"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
</asp:Content>

SelectPatientInfo.aspx.cs

protected void Page_Load(object sender, EventArgs e) {
    try {
        if (!IsPostBack) {
            dt = new DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt = AddRow(dt);
            gvPatient.DataSource = dt;
            gvPatient.DataBind();
        }
    } catch (Exception ex) {

    }
}

private DataTable AddRow(DataTable dt) {
    for (int i = 0; i < 5; i++) {
        DataRow dr = dt.NewRow();
        dr[0] = "";
        dr[1] = ""; dt.Rows.Add(dr);
    }
    return dt;
}

protected void GridPatient_DataBound(object sender, EventArgs e) {
    try {
        foreach (GridViewRow item in gvPatient.Rows) {
            HiddenField hfReferralId = (HiddenField)item.FindControl("PatientRefferalId");
            Response.write(hfReferralId.Value);
        }
    } catch (Exception ex) {

    }
}

推荐答案

我不确定代码

document.getElementById("<%=PatientRefferalId.ClientID%>")

将起作用,因为您没有一个"PatientRefferalId",但是却有很多(与gridview中的行数一样多).

will work, because you don't have only one "PatientRefferalId", but you get many (as many as numbers of rows in your gridview).

我不知道是否有更清洁的方法,但是我可以使用此javascript代码来做你想做的事

I don't know if there is a cleaner way, but I can do what you want by using this javascript code

var gv = document.getElementById("<%=gvPatient.ClientID%>");
var Rows = gv.getElementsByTagName("tr"); // Get all the rows from your gridview (rendered as html table).
// you can loop through the rows or if you know the row index, you can do:
alert(Rows[2].childNodes[0].children[0].value); // Show you the first control (the hiddenfield) of the first cell of the row #2.

这篇关于使用JavaScript/Jquery获取网格视图隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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