Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

SqlDataSourceID in templateField?

Hi,

Say I have this GridView or DetailsView with a few templateField and the GridView or DetailsViews gets populated using the SqlDataSource with SqlDataSource1(<==automatic default name)

In editItem, I place a dropdownlist and bind it with a SqlDataSource. When this happends the SqlDatasource gets placed inside of edititem tag below the dropdownlist tag with the SqlDatasource1 id, even though SqlDatasource1 has been used up already.

And this runs fine.

But I create a second dropdownlist with another sqldatasource. Its sqldatasourceid gets named as SQlDatasource1 again.

But this time, debug complains that there are duplicate names.

1) Is there a difference between SqlDatasource being place inside of edititem than outside of gridview/or detailsview??

2) Why did it run ok when i had only one dropdownlist and with two same sqldatasource ids??
but didn't work when I had two dropdownlists??

3) Why did it create same SqlDatasourceid when I place th dropdownlist inside of the template?
when it usually automatically increment the id??

Thanks.
Avatar of GreymanMSC
GreymanMSC

Do you mean something like this?  It should work fine.
<asp:SqlDataSource ID="sdsOne" runat="server"
    ConnectionString="<%$ ConnectionStrings:Sample %>"
    SelectCommand="Select ID, DataKey FROM DataKeyHolder ORDER BY ID;"
></SqlDataSource>
<asp:GridView ID="gvOne" runat="server"
    DataSourceID="sdsOne" DataKeyFields="ID"
    AutogenerateColumns="False" ShowHeader="False"
><Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:SqlDataSource ID="sdsTwo" runat="server"
                ConnectionString="<%$ ConnectionStrings:Sample %>"
                SelectCommand="Select DataKey, DataValue FROM KeyValues ORDER BY DataKey;"
            ></SqlDataSource>
            <asp:DropDownList ID="ddlTwo" runat="server"
                DataSourceID="sdsTwo"
                DataValueField="DataKey" DataTextField="DataValue"
                SelectedValue='<%# Eval("DataKey") %>'
            >
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

Open in new window

Avatar of dkim18

ASKER

Yes, something like that.
If you use sqldatasource, its default naming convention is SqlDatasource1, SqlDatasource2...3..etc.

And if you put a dropdownlist in EditItemTemplate and create a sqldatasource, it places the SqlDatasource inside of that edititemtemplate tag.
And it names the sqldatasurceid as sqldatasource1.

The sqldatasourceid for the gridview is also sqldatasource1 because it was created first.

<asp:SqlDataSource ID="SqlDatasource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:Sample %>"
    SelectCommand="Select ID, DataKey FROM DataKeyHolder ORDER BY ID;"
></SqlDataSource>
<asp:GridView ID="gvOne" runat="server"
    DataSourceID="sdsOne" DataKeyFields="ID"
    AutogenerateColumns="False" ShowHeader="False"
><Columns>
    <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
    <EditItemTemplate>
                  <asp:DropDownList ID="ddlTwo" runat="server"
                DataSourceID="sdsTwo"
                DataValueField="DataKey" DataTextField="DataValue"
                SelectedValue='<%# Eval("DataKey") %>' </asp:DropDownList>
 <asp:SqlDataSource ID="SqlDatasource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:Sample %>"
                SelectCommand="Select DataKey, DataValue FROM KeyValues ORDER BY DataKey;"></SqlDataSource>
 
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server"
            Text='<%# Bind("FirstName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
</asp:GridView>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GreymanMSC
GreymanMSC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

Thanks.
I was trying to understand why one gridview's datasource and one dropdownlist's datasource didn't have any name complict but with one gridview and two dropdownlist all having same datasource name did.

Also why would VS code it that way automatically?

Thanks