使用 JavaScript/Jquery 获取网格视图隐藏字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7789670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:19:35  来源:igfitidea点击:

get grid view hidden field value using JavaScript/Jquery

javascriptjqueryasp.net

提问by Sharanamma Jekeen

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 contextso how can I access hidden field value?

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

SelectPatientInfo.aspx

选择患者信息.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

选择PatientInfo.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) {

    }
}

回答by Matt Roy

I'm not sure the code

我不确定代码

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

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

会起作用,因为您不仅有一个“PatientRefferalId”,而且还有很多(与 gridview 中的行数一样多)。

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

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

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.

回答by user2035284

Hi This post may help you.

嗨,这篇文章可能对你有帮助。

var c = document.getElementsByTagName("table");
    for (var i = 0; i < c.length; i++) {
        if (c[i].id.indexOf("GridView1") > -1) {
            var hidd = c[i].getElementsByTagName("input");
            for (var j = 0; j < hidd.length; j++) {
                if (hidd[j].type == "hidden")
                    alert(hidd[j].id);
            }
        }
    }

And also refer following link .. its working to me..

并参考以下链接..它对我有用..

http://forums.asp.net/p/1510265/3603566.aspx/1?Re+how+to+find+gridview+hidden+label+value+from+javascript

http://forums.asp.net/p/1510265/3603566.aspx/1?Re+how+to+find+gridview+hidden+label+value+from+javascript

回答by Mukesh Prajapati

    <asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">
        <script type="text/javascript">
            function DispValue(btnShow) {
  var parentRow = $(btnShow).closest("tr");
var hiddenField=parentRow.find('input[id$=PatientRefferalId]');
 alert(hiddenField.val());

    return false;
            }
        </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" />

  <asp:LinkButton ID="lnkPopUp" runat="server" Style="font-size: 16px;" OnClientClick="return DispValue(this)"  Text="PopUp"
                                                       ></asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </asp:Content>