How to do OnClick on selected item in “dynamic” ASP.NET DropDownList without using JavaScript

Currently, there is 2 values in my DropDownList which is viewProduct and editProduct. So when the user selects one of the value, it will direct user to the particular function in ASP.NET. I have tried OnSelectedIndexChanged, but it seems like not working on the dynamic DropDownList.
Here is my code:

            ddlAction.ID = "ddlForAction";
            ddlAction.CssClass = "ddlList";
            ddlAction.CausesValidation = false;
            ddlAction.AutoPostBack = true;
            ddlAction.Items.Add(new ListItem("View","viewProduct"));
            ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
            e.Row.Cells[5].Controls.Add(ddlAction);
      
            if(ddlAction.SelectedValue == "editProduct")
            {
                editProduct();
            }
            else if(ddlAction.SelectedValue == "viewProduct")
            {
                retrieveProduct();
            }

Any idea how to solve it without using JavaScript?
My entire code:

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridView();
    }

    private void BindGridView()
    {
        MySqlConnection conn = new MySqlConnection(sqlConn);
        string sql = "SELECT prodID, prodName, stockLevel, reorderLevel, unitPrice from Product";
        MySqlCommand cmd = new MySqlCommand(sql, conn);
        MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        productList.DataSource = dt;
        productList.DataBind();
    }

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        TableCell tc = new TableCell();
        DropDownList ddlAction = new DropDownList();
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Prod ID";
            e.Row.Cells[1].Text = "Product Name";
            e.Row.Cells[2].Text = "Qty";
            e.Row.Cells[3].Text = "Reorder Level";
            e.Row.Cells[4].Text = "Price (RM)";
            e.Row.Controls.Add(tc);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var qty = Int16.Parse(e.Row.Cells[2].Text);
            var reorder = Int16.Parse(e.Row.Cells[3].Text);
            if (qty <= reorder)
            {
                e.Row.Cells[2].CssClass = "redCell";
                e.Row.Cells[3].CssClass = "redCell";
            }
            e.Row.Controls.Add(tc);
            ddlAction.ID = "ddlForAction";
            ddlAction.CssClass = "ddlList";
            ddlAction.CausesValidation = false;
            ddlAction.AutoPostBack = true;
            ddlAction.Items.Add(new ListItem("View","viewProduct"));
            ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
            e.Row.Cells[5].Controls.Add(ddlAction);
            ddlAction.SelectedIndexChanged += DDLAction_OnSelectedIndexChanged;

            if (ddlAction.SelectedValue == "editProduct")
            {
                editProduct();
            }
            else if (ddlAction.SelectedValue == "viewProduct")
            {
                retrieveProduct();
            }


        }
     }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to declare an event handler then assign it to the control before adding the control to the page.

Here is an example :

 protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        CreateControl();
    }
}

protected void CreateControl()
{
    var ddlAction = new DropDownList();
    ddlAction.ID = "ddlForAction";
    ddlAction.CssClass = "ddlList";
    ddlAction.CausesValidation = false;
    ddlAction.AutoPostBack = true;
    ddlAction.Items.Add(new ListItem("View","viewProduct"));
    ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
    ddlAction.SelectedIndexChanged += DDLAction_OnSelectedIndexChanged;
    e.Row.Cells[5].Controls.Add(ddlAction);
}

protected void DDLAction_OnSelectedIndexChanged(object sender, EventArgs e)
{
    var action = (DropDownList) sender;

    var choice = action.SelectedValue?.Trim();
    
    string script = null;

    switch (choice)
    {
        case "viewProduct":
            script = "alert('View page!');";
            break;
        case "editProduct":
            script = "alert('Edit page!');";
            break;
        default:
            script = "alert('Unmatched Value!')";
            break;
    }
    
    ScriptManager.RegisterStartupScript(this , typeof(_Default) , "TestJS" , $"<script>{script}</script>" , false);
}

use ScriptManager.RegisterStartupScript when you want to work with JS scripts. Response.Write will not parse script blocks as an executable blocks. It will throw a parse error though (you could check the browser console.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x