CodeSharp
CodeSharp

Reputation: 15

Sending java script variable value as part of PostBackUrl query string

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%>'"

Upvotes: 0

Views: 1251

Answers (2)

VDWWD
VDWWD

Reputation: 35564

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>

Upvotes: 0

Seano666
Seano666

Reputation: 2238

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 + "'"
     }

Upvotes: 0

Related Questions