We help IT Professionals succeed at work.

EditItemTemplate - Dropdownlist

innocent1973
innocent1973 asked
on
Hello Experts,
I have an editItemTemplate column with a dropdownlist. I would like to add items based on the gridview's selected column.
Is there any way to do this?

My code is like below:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        AutoGenerateEditButton="True" BackColor="White" BorderColor="#3366CC" 
        BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        DataSourceID="SqlDataSource1">
        <RowStyle BackColor="White" ForeColor="#003399" />
        <Columns>
            <asp:TemplateField HeaderText="Class">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("sSinifKodu") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Size Type">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" 
                        DataSourceID="SqlDataSource1" DataTextField="sBedenTipi" 
                        DataValueField="sBedenTipi" Height="25px" Width="126px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:FOREVERConnectionString2 %>" 
                        SelectCommand="SELECT sBedenTipi FROM IAS_ClassSizeTypes WHERE (sSinifKodu = @sSinifKodu)">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="GridView1" Name="sSinifKodu" 
                                PropertyName="SelectedRow.Columns[1].ToString()" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("sBedenTipi") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Assortmen Type"></asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:FOREVERConnectionString %>" 
        SelectCommand="SELECT sSinifKodu, sBedenTipi FROM IAS_ClassSizeTypes">
    </asp:SqlDataSource>
    </form>
</body>
</html>

Open in new window

Comment
Watch Question

Try adding a gridview updating event handler and then define your parameter value in it.

Sub gridview_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvResult.RowUpdating
        Dim rowindex As Integer = e.RowIndex
        Dim ddl As DropDownList = gvResult.Rows(rowindex).FindControl("ddlOfficeUpdate")
        SqlDataSource1.UpdateParameters("Office").DefaultValue = ddl.SelectedValue
    End Sub
 
 
Note: if you remove the parameter from teh update parameters list then you can change the above code to this:
 
 Sub gridview_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvResult.RowUpdating
        Dim rowindex As Integer = e.RowIndex
        Dim ddl As DropDownList = gvResult.Rows(rowindex).FindControl("ddlOfficeUpdate")
        SqlDataSource1.UpdateParameters.Add("Office", TypeCode.String, ddl.SelectedValue)
    End Sub

Author

Commented:
Thank you very much for your reply. I am very new at asp.net programming.
If it possible could you please write it as c# code. And :
SqlDataSource1.UpdateParameters.Add("Office", TypeCode.String, ddl.SelectedValue)
 where can I locate "office" parameter?

Regards

Author

Commented:
I have modified your code as follow:
But I get some errors
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int rowindex = e.RowIndex;

  //Dim ddl As DropDownList = gvResult.Rows(rowindex).FindControl("ddlOfficeUpdate")
      DropDownList ddl = GridView1.Rows[rowindex].FindControl("DropDownlist1");// ("dropdownlist1");
      SqlDataSource1.UpdateParameters["@sSinifKodu"].DefaultValue= ddl.SelectedValue.ToString();
    }

Open in new window

Author

Commented:
YESSSSS....
I have modified your code and got it.......

Thank you very much for your great helpsssss
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int rowindex = e.RowIndex;

  //Dim ddl As DropDownList = gvResult.Rows(rowindex).FindControl("ddlOfficeUpdate")
        DropDownList ddl = new DropDownList();
        GridView1.Rows[rowindex].FindControl("DropDownlist1");// ("dropdownlist1");
        SqlDataSource1.UpdateParameters["@sSinifKodu"].DefaultValue= ddl.SelectedValue.ToString();
    }

Open in new window