如何从GridView中的DropDownList获取选定值

时间:2013-12-31 06:05:52

标签: asp.net gridview selectedindex

我在GridView中有一个DropDownList。 GridView中可以有' n '行数,每行都有一个具有不同值的DropDownList。我正在动态填充DropDownLists,所有值都在DropDownLists中正确填充。

当我为每行更改DropDownList的选定值并按下按钮保存这些值时,我总是得到0索引的值而不是我选择的值。

我发现很多像这样的问题,但没有一个能帮助我。是什么原因导致它无法从DropDownList中获取正确的选定值?有什么想法吗?

当我删除GridView并使用DropDownList时,它可以正常工作。

4 个答案:

答案 0 :(得分:0)

这样做....

在gridview中放置一个链接按钮,并将其命令参数设置为这样的rowdatabount事件...并为该链接按钮设置任何命令名。

在此代码中,我设置了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;

}

} 

答案 1 :(得分:0)

我是从这里的教程实现的     Link

答案 2 :(得分:0)

试试这个

<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

答案 3 :(得分:0)

我认为这会对您有所帮助

    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;
        }
    }