SueSaya
SueSaya

Reputation: 57

How to find control in ControlParameter? asp.net c#

I use EditItemTemplate to create DropDownList in GridView for edit data in database but I can't get value form DropDownList when I select value and click edit button.

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    SqlDataAdapter dtAdapter;
    DataTable dt = new DataTable();

    strSQL2 = "SELECT distinct(gp.RespIndexID) RespIndexID,gr.RespDesc " +
            "FROM Login as gbl " +
            "LEFT JOIN Profile gp ON gbl.EmpID=gp.EmpID " +
            "LEFT JOIN ResponeIndex gr ON gp.RespIndexID=gr.IndexID " +
            "WHERE gbl.EmpDept=" + DropDownList3.SelectedValue.ToString() + " AND gbl.EmpSection=" + DropDownList4.SelectedValue.ToString() + " AND gbl.EmpUnit=" + DropDownList1.SelectedValue.ToString() + " AND gbl.Status is Null";
    try
    {
        dtAdapter = new SqlDataAdapter(strSQL2, objConn);
        dtAdapter.Fill(dt);

        for (int i = 0; dt.Rows.Count > i; i++)
        {

            if (GridView1.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlPosition = (DropDownList)e.Row.Cells[3].FindControl("ddlPosition");
                HiddenField HiddenField1 = (HiddenField)e.Row.Cells[3].FindControl("HiddenField1");
                ddlPosition.Items.Add(new ListItem((string)dt.Rows[i]["RespIndexID"].ToString(), dt.Rows[i]["RespIndexID"].ToString()));

                if (Page.IsPostBack == true)
                {
                    ddlPosition.SelectedValue = HiddenField1.Value;
                }
                //er.NewValues[i] = ddlPosition.SelectedValue.ToString();
            }
        }
    }
    catch (Exception err)
    {
        //Response.Write(strSQL2 + "<br />" + err); 
    }
}

//Code in GridView

<asp:TemplateField HeaderText="RespDesc" SortExpression="RespDesc">
                        <EditItemTemplate>
                            <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("RespIndexID") %>'></asp:HiddenField>
                            <asp:DropDownList ID="ddlPosition" runat="server" AutoPostBack="false" AppendDataBoundItems="True"></asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("RespIndexID") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle Width="100px" />
                        <ItemStyle HorizontalAlign="Left" />
                    </asp:TemplateField>

//Code update in DataSource

UpdateCommand="UPDATE Profile SET RespIndexID = @ddlPosition WHERE (EmpID = @EmpID)">
<UpdateParameters>
  <asp:ControlParameter ControlID="ddlPosition" Name="RespIndexID" PropertyName="SelectedValue" Type="String" />
 <asp:Parameter Name="EmpID" />                    

Upvotes: 0

Views: 969

Answers (1)

Blachshma
Blachshma

Reputation: 17385

First of all, you should note that your code is open for SQL Injections, so start off by Parametrizing you queries.

Now for your question - you can do it in the codebehind by using the FindControl method to get your DropDownList. Register to the RoweCommand event:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

     if (e.CommandName == "Edit")
     {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

            DropDownList ddl = (DropDownList)row.FindControl("ddlPosition");

            // Do whatever you want with the dropdownlist
     }
}   

Where "Edit" is the name of your command you used in the LinkButton

Upvotes: 1

Related Questions