简体   繁体   中英

How to pass value to a dropdownlist in asp.net?

I have a dropdownlist inside a gridview, my gridview has a datatable datasource and when i try to pass the value to the datasource, it doesnt handle the value.

 SqlCommand com2 = new SqlCommand("select MRPFABRIC.GoodForHow, MRPFABRIC.MrpQty, allowance, ArticleFabricAssign.Consumption,(select SUBDETAILS.Description from SUBDETAILS where SubDetailsID = MRPFABRIC.ColorName) as Color,(select SUBDETAILS.Description from SUBDETAILS where SubDetailsID = MRPFABRIC.FabricName) as Fabric from MRPFABRIC join ARTICLE on ARTICLE.ControlNo = MRPFABRIC.ControlNo join ARTICLEFABRICASSIGN on ARTICLEFABRICASSIGN.ArtFabID = MRPFABRIC.ArtFabId join FABRIC on FABRIC.FabricID = ARTICLEFABRICASSIGN.FabricID join mrp on MRP.MRPNo = MRPFABRIC.MRPNo where MRP.MRPNo =  @fmno and ARTICLE.ArticleNo = @articleno", con);
        com2.Parameters.AddWithValue("@fmno", txtMRPNo.Text);
        com2.Parameters.AddWithValue("@articleno", ddlArtNo.SelectedItem.Text);
        DataTable dt2 = new DataTable();
        SqlDataAdapter adapt = new SqlDataAdapter(com2);
        adapt.Fill(dt2);
        GridView1.DataSource = dt2;
        GridView1.DataBind();

and the passing of values is here

 DropDownList ddlfab = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("ddlConst");
                        DropDownList ddlcol = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlColor");
                        TextBox txtgfhm = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox5");
                        TextBox txtconsump = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox6");
                        TextBox txtallow = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("TextBox7");
                        Label lblmrpq = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label1");

                        DataRow row2 = dt2.Rows[0];
                        ddlfab.SelectedItem.Text = dt2.Rows[i][5].ToString();
                        txtgfhm.Text = dt2.Rows[i][0].ToString();
                        txtconsump.Text = dt2.Rows[i][3].ToString();
                        txtallow.Text = dt2.Rows[i][2].ToString();
                        lblmrpq.Text = dt2.Rows[i][1].ToString();
                        ddlcol.Text = dt2.Rows[i][4].ToString();

the ddlcol is the one who doesnt handles the value. i tried ddlcol.SelectedItem.Text but it returns null reference exception though the row i tried to pass has a value. ddlCol.SelectedValue also returns nothing.

Try this

        ddlcol.DataSource= dt2            
        ddlcol.DataTextField = "UserID";
        ddlcol.DataValueField = "UserID";
        ddlcol.DataBind();

you really need to check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM