• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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.
0
dkim18
Asked:
dkim18
  • 2
  • 2
1 Solution
 
GreymanMSCCommented:
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

0
 
dkim18Author Commented:
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

0
 
GreymanMSCCommented:
If you use the _same_ ID for datasources (or any control for that matter) inside _and_ outside the template, then scope rule _should_ apply.  That is, any reference to an ID should look for a match inside the template's scope before looking outside.  
 
Still, the operating word is "should".   So if you are finding that the compiler is getting confused despite this, then the simplest solution is to rename the controls more uniquely.  
 
Actually, it is a good idea to never use the default names, anyway.  Best practice is to always give your controls meaningful and unique names.  It makes debugging so much easier.
 
(For one thing: If an error is raised telling you there is a problem with "SqlDatasource1", it's much less frustrating to only have one place that needs to be examined.)
0
 
dkim18Author Commented:
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
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now