-1

I have a content VB page where I try to programmatically change Select in SqlDataSource.

My code:

<asp:Content ID="Content6" ContentPlaceHolderID="MainContent" Runat = "server"> 
<table width="630">
<tr> <td>
Dim Label2 As New Label()
Label2.Text = "Bro"
</td>

<td>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
 ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
 SelectCommand="SELECT * FROM [Docs1] WHERE ([Type] = @Type)">
 <SelectParameters>
 <asp:ControlParameter Name="Type" 
 ControlID="Label2"
 PropertyName="Text" 
  />

 </SelectParameters>

 </asp:SqlDataSource>
 </td>
 <td>
 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
 DataSourceID="SqlDataSource2" CellPadding="4" ForeColor="#333333" 
 GridLines="None"  Font-Names="Arial" 
 >

 <AlternatingRowStyle BackColor="White" Font-Size="XX-Small"  Font-Bold="False" 
 ForeColor="#284775" Font-Names="Times New Roman" Font-Underline="False" />

 <Columns>
  .......
 </asp:GridView>

SqlDataSource does not get Label2.Text. What I do not do correctly? I must change select parameter ( from "BLah1" to "Blah2" etc ) several times on the page WITHOUT any action from the user, so no buttons etc and essentially no events, except page_Load. I want to use invisible Label and change their Text and in this way - change select.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61

1 Answers1

0

You should also add your label to the form and set its ID property. I.e.

Dim Label2 As New Label()
Label2.ID = "Label2" 
Form.Controls.Add(Label2)
Label2.Text = "Bro"
platon
  • 5,310
  • 1
  • 22
  • 24
  • Thaks platon, but now it says: – user1692508 Sep 30 '12 at 10:58
  • Thanks platon but now it says - could not find control 'Label2' in Control Parameter 'Type' – user1692508 Sep 30 '12 at 11:04
  • Most likely, your label is positioned inside the UserControl or any other INamingContainer object. It means that its UniqueID won't be Label2 and thus the control cannot be found by ASP.NET on the form. A possible solution is to change the parameter's ControlID property and map it to the Label's UniqueID. The second idea is that you create your label too late, after the binding process performed. Please try to create the label at the Page_Load method. – platon Sep 30 '12 at 12:06
  • Tried to create Label in Page_Load. The same result - could not find control 'Label2' in Control Parameter 'Type'.I see a lot of questions about changing parameters programmatically, but no examples of doing it WITHOUT actions by user. Can you please give me a link(s) to detailed explanations with examples? And please - maybe a link to explanation of logic behind controls, because otherwise it solves maybe a problem, but does not explain why it can be solved so – user1692508 Sep 30 '12 at 13:30
  • can you upload a sample somewhere so that I can take a look? For now, I've no idea of what is happening in your app. My tests work properly with this code. – platon Sep 30 '12 at 16:30
  • In Docs1.aspx.vb I have: Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim Label2 As New Label() Label2.ID = "Label2" Form.Controls.Add(Label2) Label2.Text = "Bro" End Sub – user1692508 Oct 02 '12 at 08:08
  • Thank you platon, No it says there can be only one form on the page. Indeed in master page i have also a form and i need it there, so I must learn more and maybe move to LINQ to SQL. I understand that this clumsy approach to prorammatically changing values of parameters in Select is the price for nice solutions to such problems whan user DO something, but not when something is needed – user1692508 Oct 02 '12 at 08:33