1

Can an ASP.Net asp:ControlParameter ControlID be a public shared variable instead of an ASP:label?

We were using an asp:label as a parameter for a DataSource but now want to use a public shared variable instead of the label.

This is the markup of the parameter using the asp:label.

<asp:ControlParameter 
    ControlID="LabelCheckBoxMonday" 
    Name="DayOfWeekMonday" 
    PropertyName="Text" 
    Type="String" />

We added this public shared variable in the code-behind.

Public Shared blnDayOfWeekMonday As Boolean

We changed the markup for the parameter to this.

<asp:ControlParameter 
    ControlID="blnDayOfWeekMonday" 
    Name="DayOfWeekMonday" 
    PropertyName="Text" 
    Type="String" />

This is the coding that places values into the variable. The original coding that used to do that for the label is commented out.

Protected Sub ImageButtonInsertDayOfWeekMonday_Click(sender As Object, e As ImageClickEventArgs)

    Dim imgTheImageButton As New ImageButton

    imgTheImageButton = DetailsView.FindControl("ImageButtonInsertDayOfWeekMonday")

    If imgTheImageButton.ImageUrl = "../../Images/checked.png" = True Then

        imgTheImageButton.ImageUrl = "../../Images/unchecked.png"
        '            LabelCheckBoxMonday.Text = False
        blnDayOfWeekMonday = False
    Else

        imgTheImageButton.ImageUrl = "../../Images/checked.png"
        '           LabelCheckBoxMonday.Text = True
        blnDayOfWeekMonday = True
    End If
End Sub

All of this is part of the following InsertCommand:

    InsertCommand=
        "INSERT INTO [TeacherSchedule] 
            ([DayOfWeekMonday], 
             [DayOfWeekTuesday], 
             [DayOfWeekWednesday], 
             [DayOfWeekThursday], 
             [DayOfWeekFriday], 
             [DayOfWeekSaturday], 
             [DayOfWeekSunday], 
             [StartTime],
             [EndTime],
             [ClassID],
             [TeacherID]) 
        VALUES (@DayOfWeekMonday, 
                @DayOfWeekTuesday, 
                @DayOfWeekWednesday, 
                @DayOfWeekThursday, 
                @DayOfWeekFriday, 
                @DayOfWeekSaturday, 
                @DayOfWeekSunday, 
                @StartTime, 
                @EndTime,
                @ClassID,
                @TeacherID)" 

When the web form is running nothing happens after changing it to the public shared variable.

Can you tell me what else I need to do to proceed?

* Update *

Using Marks suggestion I found out how to do an asp:QueryStringParameter but still don't know how to populate it with a value. This is the parameter as an asp:QueryStringParameter

<asp:QueryStringParameter
    Name="DayOfWeekMonday" 
    QueryStringField="QSDayOfWeekMonday" />

How do I populate QSDayOfWeekMonday in the ImageButtonInsertDayOfWeekMonday_Click sub routine?

I tried:

QSDayOfWeekMonday = False

but got a "not declared" error.

* Full markup and code-behind coding *

http://pastebin.com/embed_js.php?i=kye3c2U8

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • blnDayOfWeekMonday is not a Control so no you cannot use ControlID. To be honest both ways are pretty bad. Why not use a querystring parameter? – Mark Homer Apr 25 '13 at 14:49
  • Thanks for the reply Mark. Can you show us how to use a querystring parameter along with the needed markup and code-behind coding to store true or false into @DayOfWeekMonday ? I'm also including the InsertCommand so you know what we are trying to do. – Emad-ud-deen Apr 25 '13 at 14:58
  • Thanks for the pastebin sample. DayOfWeekMonday, as a static, means everyone will share the same value. I don't think that's what you want. Also, I believe DefaultValue is a string, so you need to make sure it's a string value. – Brian Mains Apr 25 '13 at 18:48

2 Answers2

2

Another option is to use a Parameter as:

<asp:Parameter Name="DateOfWeekMonthly" />

And in code set the DefaultValue property to the value you want to specify, as in:

DataSourceControl1.Parameters["DateOfWeekMonthly"].DefaultValue = someVariable;

This has worked for me.

You could establish this event in the Selecting event that fires; this event fires before the select happens; therefore, you can establish the boolean value. I believe here, you can add it to the collection of values defined in the event argument.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Thanks for the answer. I used SqlDataSourceDetails.InsertParameters("DayOfWeekMonday").DefaultValue = False but would like to be able to actually set the value to false instead of just setting the default value. Can this be done? – Emad-ud-deen Apr 25 '13 at 16:10
  • Try using "False" or Boolean.FalseString or "0" (meaning false as booleans are a bit in sql). I'm not 100% sure which exact one of these options will work (as I don't know if I used this trick with a boolean parameter), but one of these will do the trick. – Brian Mains Apr 25 '13 at 17:28
  • I found that I could add Type="Boolean" to the parameter but since I want to allow the user to toggle between true and false setting the default value may work the first time the code is run. When I ran the code, the parameter ended up with a null value. – Emad-ud-deen Apr 25 '13 at 17:37
  • I believe you have to establish this on every postback, before the datasource triggers the select operation. It's hard to debug those types of issues without seeing code. If you could post and update above, it would help me to see what's going on. – Brian Mains Apr 25 '13 at 17:48
  • Can you show us how to do that? Can I post all of the code here on stackoverflow? Thanks. – Emad-ud-deen Apr 25 '13 at 17:49
  • Just during Page Init or Page Load, you would need to establish the value. You could try attaching to the Selecting event also, via http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selecting.aspx – Brian Mains Apr 25 '13 at 17:51
  • I just tried to add all of the coding but it went over the limit I can post. Can I email it to you? I hope stackoverflow will allow that. – Emad-ud-deen Apr 25 '13 at 18:02
  • Put it on pastebin or codepaste sites, and attach the link to your main post. – Brian Mains Apr 25 '13 at 18:06
0

I used this shorter way:

<asp:Parameter Name="DateOfWeekMonthly" Type="Boolean" DefaultValue="true" />

This is also working.

wexivi
  • 1
  • 2