Sending java script variable value as part of PostBackUrl query string

1.2k views Asked by At

I need to send a parameter value into a query string of a PostBackUrl method within asp button.

The value I need is already being captured within a java script function shown below.

How do I send the value in hiddId as part of URL ? The below method isn't working. Can someone please help ?

   <script language="javascript" type = "text/javascript">
        function btn_Selected() {
           var hiddId = null;
           $('#<%=GridView1.ClientID %>').find('input[type=radio]:checked').each(function () {
               hiddId = $(this).closest('tr').find('input[type = "hidden"]').val();
           });

     }

 <asp:Button ID="Btn_Update" runat="server" Text="Update" PostBackUrl="Screen_Edit.aspx?CustID='<%=hiddId%>'"
2

There are 2 answers

2
Seano666 On BEST ANSWER

Instead of a post-back, just redirect using JavaScript.

function btn_Selected() {
           var hiddId = null;
           $('#<%=GridView1.ClientID %>').find('input[type=radio]:checked').each(function () {
               hiddId = $(this).closest('tr').find('input[type = "hidden"]').val();
           });
           window.location.href = "Screen_Edit.aspx?CustID='" + hiddId + "'"
     }
1
VDWWD On

If you look at the HTML source of the page, the button will have an javascript onclick event that looks something like this javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$mainContentPane$Btn_Update&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Screen_Edit.aspx?CustID=&quot;, false, false))

All we have to do is find a way to insert your variable after ?CustID=.

You can replace the onclick value of the button with the .attr() function of jQuery and do a search and replace to insert your variable.

<script type="text/javascript">
    $(document).ready(function () {
        var hiddId = $(this).closest('tr').find('input[type = "hidden"]').val();
        $("#<%=Btn_Update.ClientID %>").attr("onclick", $("#<%=Btn_Update.ClientID %>").attr("onclick").replace("?CustID=", "?CustID=" + hiddId));
    });
</script>