0

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%>'"
CodeSharp
  • 15
  • 4

2 Answers2

0

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 + "'"
     }
Seano666
  • 2,238
  • 1
  • 15
  • 14
  • That worked, although now I have a new problem.. the CustID is being passed with single quote. So, when I try to get the parameter in code behind, the Request.QueryString["CustID"] returns '3001' instead of 3001 – CodeSharp Sep 01 '16 at 19:14
  • I thought that's how you wanted it, so just go window.location.href = "Screen_Edit.aspx?CustID=" + hiddId – Seano666 Sep 01 '16 at 20:20
0

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>
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Even though I didn't try the jQuery method, its still good to know the way the attributes can be changed. Thanks..!! – CodeSharp Sep 02 '16 at 14:23