Unnikrishnan.S
Unnikrishnan.S

Reputation: 185

how to get button click id from other aspx page?

I have a button in an aspx page,while clicking this button will open another aspx page.I want to know how can i get this button clik id from opened aspx page.

Codes

<asp:Button 
        ID="Button1" 
        runat="server" 
        Text="Cycle Test 1" 
        class="studentdetbtn"
        OnClientClick="window.open('internalmark.aspx', 'Internal Mark');" 

        />

This button is in markdetails.aspx page,and i want to access this button id from internalmark.aspx page while clicking.How can i possible?any idea?

Upvotes: 0

Views: 1078

Answers (2)

user4222558
user4222558

Reputation:

You can pass button click id as a parameter in querystring and then access that querystring value from other page.

Here is code for your page1:

 <asp:Button ID="Button1" 
        runat="server" 
        Text="Cycle Test 1" 
        class="studentdetbtn"
        OnClientClick="return OpenPage(this);" />


    <script>
        function OpenPage(elemt) {
            var id = $(elemt).attr("id");
            window.open('internalmark.aspx?param=' + id , 'Internal Mark');
        }
    </script>

Here is code for your page2 i.e "internalmark.aspx":

 <script>
  $(document).ready(function () {
            var qs = getQueryStrings();
            var GetButtonId= qs["param"];
            alert("here is the button id: "+ GetButtonId)

        });
        function getQueryStrings() {
            var assoc = {};
            var decode = function (s)
            {
                return   
                decodeURIComponent(s.replace(/\+/g, " ")); 
            };
            var queryString = location.search.substring(1);
            var keyValues = queryString.split('&');

            for (var i in keyValues) {
                var key = keyValues[i].split('=');
                if (key.length > 1) {
                    assoc[decode(key[0])] = decode(key[1]);
                }
            }
            return assoc;
        }
 </script>

Upvotes: 2

D. Jayesh
D. Jayesh

Reputation: 17

If you look into button Click method then protected void button1_click (object sender, EventArgs e), the sender contains the ID of the Button.

You can do Button button = (Button)sender; string buttonId = button.ID;

store this buttonId variable in Session or ViewState or pass in query string

Upvotes: -1

Related Questions