How to get selected value from DropDownList which is inside GridView

拟墨画扇 提交于 2019-12-08 11:18:50

问题


I have a DropDownList inside GridView. There can be 'n ' number of rows in a GridView with each row having a DropDownList with different values. I am populating DropDownLists dynamically and all the values are populated properly in DropDownLists.

When I change the selected value of DropDownList for each row and press the button to save these values, I always get the value of 0 index instead of the value that I have selected.

I found lot of questions like this but none of them helps me. What's the reason it doesn't get the right selected value from DropDownList? Any ideas?

When I remove the GridView and just use DropDownList then it works fine.


回答1:


do like this....

put a link button in gridview and set its commandargument a rowdatabount event like this... and set any commandname for that linkbutton.

In this code i have set CommandName ="test"

int count;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//DataRowView oRow = (DataRowView)e.Row.DataItem;

LinkButton lnkPostType = (LinkButton)e.Row.FindControl("linkbpostype");

lnkPostType.CommandArgument = count.ToString();

count++;

}

}

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "test")

{


int RowIndex = int.Parse(e.CommandArgument.ToString().Trim());

DropDownList ddlNew = (DropDownList)GridView1.Rows[RowIndex].FindControl("ddlst");

string abc = ddlNew.SelectedValue;

}

} 



回答2:


I implemented this from a tutorial here Link




回答3:


I think this will help you

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string stateID = GridView1.DataKeys[e.NewEditIndex]["StateID"].ToString();
        string cityID = GridView1.DataKeys[e.NewEditIndex]["CityID"].ToString();
        // Edit Event
        GridView1.EditIndex = e.NewEditIndex;
        populateData();

        // Here populate data for dropdown state
        DropDownList ddStateEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddState");
        if (ddStateEdit != null)
        {
            ddStateEdit.DataSource = populateState();
            ddStateEdit.DataTextField = "StateName";
            ddStateEdit.DataValueField = "StateID";
            ddStateEdit.DataBind();
            ddStateEdit.SelectedValue = stateID;
        }

        DropDownList ddCityEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddCity");
        if (ddCityEdit != null)
        {
            ddCityEdit.DataSource = populateCity(Convert.ToInt32(stateID));
            ddCityEdit.DataTextField = "CityName";
            ddCityEdit.DataValueField = "CityID";
            ddCityEdit.DataBind();
            ddCityEdit.SelectedValue = cityID;
        }
    }



回答4:


Try this

<asp:TemplateField HeaderText="XYZ">
  <ItemTemplate>
    <asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" />
  </ItemTemplate> 
</asp:TemplateField>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            con.Open();
            var ddl = (DropDownList)e.Row.FindControl("ddlState");
            int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
            SqlCommand cmd = new SqlCommand("select * from State where CountryID = @CountryId", con);
            cmd.Parameters.Add("@CountryID", CountryId);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "StateName";
            ddl.DataValueField = "StateID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {

  DropDownList ddl= (DropDownList )GridView1.Rows[e.RowIndex].FindControl("ddlState");
  string selectedvalue=ddl.selectedvalue;
  //My custom code to change last moment values with that selected from DropDownList 
  e.NewValues.Add("State", selectedvalue);

 }

Source



来源:https://stackoverflow.com/questions/20852619/how-to-get-selected-value-from-dropdownlist-which-is-inside-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!