[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

datagrid dropdownlist..

Posted on 2008-11-13
7
Medium Priority
?
421 Views
Last Modified: 2013-12-17
this is the same code am using  without a datagrid , i mean this code works fine indivudally,

in other pagee i ve datagrid ,in edit template i ve three dropdown list , same result to b shown . as in code. but am not able to find id of dropdownlist . and also how can i do , selected change of dropdownlist in datagrid  .......??
protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = ddlcity.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
 
        SqlCommand recipename = new SqlCommand("select hid,recipe from recipetable where cid='" + code + "'", con1);
        SqlDataAdapter da_recipe = new SqlDataAdapter(recipename);
        DataSet ds = new DataSet();
        da_recipe.Fill(ds);
        ddlrecipe.DataSource = ds;
        ddlrecipe.DataTextField = "recipe";
        ddlrecipe.DataValueField = "hid";
        ddlrecipe.Items.Clear();
        ddlrecipe.DataBind();
 
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
 
    }
 
 
protected void ddlrecipe_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = ddlrecipe.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
----------------------------------------

Open in new window

0
Comment
Question by:Rajar Ahmed
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
moseack earned 1000 total points
ID: 22948447
Since you are adding the DDL to a dynamic data control, you will probably have more than one instance (e.g. one for each line of data). So the ddlCity & ddlres & ddlrecipe references are not relevant.
You should use the "sender" object referrence to find out who rised the event.

DropDownList ddl = (DropDownList)sender;

Open in new window

0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 1000 total points
ID: 22948540
I think if u post the code it might be more relevant.

But for obtaining the ddl in datagrid u'll have to write in the ItemDatabound event of the datagrid.


DropdownList ddl=e.Item.FindControl("ddlregion");
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22948569
Its a reall big code....
I had pasted the necessary things...
If its not enough i ll posttt the entire page....


I want the selected changed of dropdown list should happen inside the datagrid....
i dunno how to proceed.....
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="fid"
            OnCancelCommand="DataGrid1_CancelCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnUpdateCommand="DataGrid1_UpdateCommand"
            OnEditCommand="DataGrid1_EditCommand" 
            Style="left: 68px; position: relative; top: 199px" AllowPaging="True" CellPadding="4" OnPageIndexChanged="DataGrid1_PageIndexChanged" ForeColor="#333333" GridLines="None" >
            <Columns>
                <asp:TemplateColumn HeaderText="City">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative" DataSourceID="SqlDataSource1" DataTextField="city" DataValueField="cid" AppendDataBoundItems="true" SelectedItem='<%# Bind("city") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur City" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString2 %>"
                            SelectCommand="SELECT * FROM [citytable]"></asp:SqlDataSource>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="DropDownList1"
                            ErrorMessage="Please Select any City" Style="position: relative" InitialValue="0">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="recipe">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Style="position: relative" Text='<%# Bind("recipe") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" Style="position: relative" DataSourceID="SqlDataSource2" DataTextField="recipe" DataValueField="hid" AppendDataBoundItems="true" SelectedItem='<%# Bind("resipe") %>' OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur Recipe" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString3 %>"
                            SelectCommand="SELECT [hid], [recipe] FROM [recipetable]"></asp:SqlDataSource>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="DropDownList2"
                            ErrorMessage="Please Select Any Recipe" InitialValue="0" Style="left: 0px; position: relative;
                            top: -2px" Width="11px">* </asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="restaurant">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Style="position: relative" Text='<%# Bind("resname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" Style="position: relative" AppendDataBoundItems="true"  DataSourceID="SqlDataSource3"  DataTextField="resname" DataValueField="rid" SelectedItem='<%# Bind("resname1") %>'>
                        <asp:ListItem Text="Change Your restaurant" Value="0"></asp:ListItem>
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString %>"
                            SelectCommand="SELECT [rid], [resname] FROM [resttable]"></asp:SqlDataSource>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="DropDownList3"
                            ErrorMessage="Please Select any Restaurant" InitialValue="0" Style="position: relative">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="Food">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Style="position: relative" Text='<%# Bind("fname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("fname") %>'  Style="position: relative"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                            ErrorMessage="*" Style="position: relative"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="FoodType">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Style="position: relative" Text='<%# Bind("ftype") %>'></asp:Label>
                    </ItemTemplate>
                   
        </tr>
    </table>
 
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        DropDownList ddlcity = (DropDownList)sender;
        DropDownList ddlrecipe = (DropDownList)sender;
        DropDownList ddlres = (DropDownList)sender;
        string code = ddlcity.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
 
        SqlCommand recipename = new SqlCommand("select hid,recipe from recipetable where cid='" + code + "'", con1);
        SqlDataAdapter da_recipe = new SqlDataAdapter(recipename);
        DataSet ds = new DataSet();
        da_recipe.Fill(ds);
        ddlrecipe.DataSource = ds;
        ddlrecipe.DataTextField = "recipe";
        ddlrecipe.DataValueField = "hid";
        ddlrecipe.Items.Clear();
        ddlrecipe.DataBind();
 
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
 
 
        DropDownList ddlrecipe = (DropDownList)sender;
        DropDownList ddlres = (DropDownList)sender;
        string code = ddlrecipe.SelectedValue.ToString();
        string constr = ConfigurationManager.AppSettings["cons"];
        SqlConnection con1 = new SqlConnection(constr);
        SqlCommand restname = new SqlCommand("select resttable.*,resname from resttable where hid='" + code + "'", con1);
        SqlDataAdapter da_restname = new SqlDataAdapter(restname);
        DataSet ds1 = new DataSet();
        da_restname.Fill(ds1);
        ddlres.DataSource = ds1;
        ddlres.DataTextField = "resname";
        ddlres.DataValueField = "rid";
        ddlres.Items.Clear();
        ddlres.DataBind();
    }
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 1000 total points
ID: 22948590
I think the above code should work fine.

The only thing that I can see is that the autopostback of the dropdownlist has not been set to true.

Try setting that for the dropdownlists
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22948626
Am getting this error...

Both DataSource and DataSourceID are defined on 'DropDownList1'.  Remove one definition.
0
 
LVL 7

Assisted Solution

by:moseack
moseack earned 1000 total points
ID: 22948650
Since you are changing the DataSource object explicitly, you should set the DataSourceID to null.
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22948851
I removed datasourceid  but still get that error message..
asp:TemplateColumn HeaderText="City">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" Style="position: relative" DataSourceID="SqlDataSource1" DataTextField="city" DataValueField="cid" AppendDataBoundItems="true" SelectedItem='<%# Bind("city") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Text="Change ur City" Value="0" />
                        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tonivtreepConnectionString2 %>"
                            SelectCommand="SELECT * FROM [citytable]"></asp:SqlDataSource>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="DropDownList1"
                            ErrorMessage="Please Select any City" Style="position: relative" InitialValue="0">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="recipe">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Style="position: relative" Text='<%# Bind("recipe") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" Style="position: relative"  AppendDataBoundItems="true" >
                        <asp:ListItem Text="Change ur Recipe" Value="0" />
                        </asp:DropDownList>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="DropDownList2"
                            ErrorMessage="Please Select Any Recipe" InitialValue="0" Style="left: 0px; position: relative;
                            top: -2px" Width="11px">* </asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="restaurant">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Style="position: relative" Text='<%# Bind("resname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" Style="position: relative" AppendDataBoundItems="true"  >
                        <asp:ListItem Text="Change Your restaurant" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="DropDownList3"
                            ErrorMessage="Please Select any Restaurant" InitialValue="0" Style="position: relative">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </asp:TemplateColumn>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question