0

I need to click a button that is inside a gridview from the codebehind. I think the best approach will be to create a javascript function in codebehind, something like the second solution i tried below. I will appreciate any help

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Accepted")
        {

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

       //find button

            Button btnEsc = (Button)row.FindControl("btnEsc");

       //here I would like to simulate a click on this button, so far no luck

        btnEsc.Click(); // this is wrong

         }
      }

I also try this but it doesn't find the button: I dont know how to find the button inside the gridview

           System.Text.StringBuilder sbScript = new System.Text.StringBuilder("");
           sbScript.Append("document.getElementById('btnEsc').click();");
           ScriptManager.RegisterStartupScript(this, GetType(), "ClientScript", sbScript.ToString(), true);
13
  • 1
    do you really need to simulate the click or do you just want to execute something that the button would call if clicked? Commented Nov 7, 2014 at 16:15
  • 1
    then you just want to open a new lightbox, just call the javascript function that creates the lightbox and you're done stackoverflow.com/questions/5731224/… or other than the lightbox do you need something else? Commented Nov 7, 2014 at 16:20
  • 1
    Why not use javascript/jquery to do this? If you still need to call code-behind, make an ajax call and then open the lightbox on success. I don't see why you need such a convoluted solution when all you want to do is pass data to the lightbox. JS is perfect for this. Commented Nov 7, 2014 at 16:33
  • 1
    @Carlos, you can directly put all js code into a function say abc(arg), all js code implies, the JS code written for btnEsc OnClientClick, and then just use same RegisterStartupScript with params. It will work then. Commented Nov 7, 2014 at 16:37
  • 1
    Yep, use jquery. See solution, based on the same question you had then, just for demonstration... Commented Nov 7, 2014 at 16:47

2 Answers 2

1

When you open the lightbox, pass in the Id to a hidden Field. You can then read it out later. Example from ListView here.

protected void lvProjectServices_ItemCreated(object sender, ListViewItemEventArgs e)
    {

            if (e.Item.DataItem != null)
            {
                Whatever data = (Whatever)e.Item.DataItem;

                PlaceHolder objPlc3 = (PlaceHolder)e.Item.FindControl("phEdit");

                LinkButton link3 = new LinkButton();
                link3.Text = "<i class=\"table-edit\"></i>";
                link3.ID = "lbEditServer" + data.Id.ToString();
                link3.CommandName = "Edit";
                link3.CommandArgument = data.Id.ToString();
                link3.Click += link_Click;
                objPlc3.Controls.Add(link3);

            }
        }


    void link_Click(object sender, EventArgs e)
    {

        LinkButton btn = (LinkButton)sender;

        int Id = int.Parse(btn.CommandArgument.ToString());

        txtProjectServiceId.Value = Id.ToString();

            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

            if (!scriptManager.IsClientScriptBlockRegistered("openSvcModal"))
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "openSvcModal", "$('select').select2(); $('#editProjectService').modal();", true);
            }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Code behind:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btn.Text = "Refreshed";
        if (Request.Params["btnPressed"] != null && Request.Params["btnPressed"] == "true")
        {
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", string.Format("$('#{0}').click()", btn.ClientID), true);
        }
    }

    protected void btn_Click(object sender, EventArgs e)
    {

        btn.Text = "Not Refreshed";
        lbl.Text = "Not Refreshed";
        System.Threading.Thread.Sleep(1000);
        ////to refresh the page
        Page.Response.Redirect(HttpContext.Current.Request.Url.ToString()+"?btnPressed=true", true);
    }
}

Load jquery in aspx page:

<body>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" ID="btn" OnClick="btn_Click" PostBackUrl="/WebForm1.aspx" />
        <asp:Label runat="server" ID="lbl"> </asp:Label>
    </div>
    </form>
</body>

You should really learn jquery, its a great tool. g/l.

Comments

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.