1

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) {

    }
}
2
  • provide some code to find your issue. Commented Oct 17, 2011 at 5:23
  • Not everyone will know what grid view is, or how it creates a hidden field value. Commented Oct 17, 2011 at 5:31

3 Answers 3

3

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).

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.
Sign up to request clarification or add additional context in comments.

1 Comment

If it has answered the question, it would be great to get my first accepted answer! ;)
0

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

Comments

0
    <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>

1 Comment

Please provide an explanation with your code, so the OP can learn from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.