Hi, sorry still learning how to do things in asp.

Basically I have a form that I set up using the insert template in visual studio.

where the code looks like this

<InsertItemTemplate>
            &nbsp;<table class="style15">
                <tr>
                    <td class="style18">
                        SitesAffected:
                    </td>
                    <td class="style16">
                        Description:</td>
                </tr>
                <tr>
                    <td class="style19">
                        <asp:CheckBoxList ID="SitesCheckBoxList" runat="server" AutoPostBack="True" 
                            Text='<%# Bind("SitesAffected") %>'>
                          
                            <asp:ListItem>Manchester</asp:ListItem>
                            <asp:ListItem>Newcastle</asp:ListItem>
                                                        
                        </asp:CheckBoxList>
                    </td>
                    <td class="style17">
                        <asp:TextBox ID="DescriptionTextBox" runat="server" Height="146px" 
                            Text='<%# Bind("Description") %>' Width="239px" />
                    </td>
                </tr>
                <tr>
                    <td class="style20">
                        Creator:</td>
                    <td>
                        SendTo:</td>
                </tr>
                <tr>
                    <td class="style20">
                        <asp:TextBox ID="CreatorTextBox" runat="server" Text='<%# Bind("Creator") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="SendToTextBox" runat="server" Text='<%# Bind("SendTo") %>' />
                    </td>
                </tr>
                <tr>
                    <td class="style20">
                        TechnicianDealing:</td>
                    <td>
                        BulletinType:</td>
                </tr>
                <tr>
                    <td class="style20">
                        <asp:TextBox ID="TechnicianDealingTextBox" runat="server" 
                            Text='<%# Bind("TechnicianDealing") %>' />
                    </td>
                    <td>
                                                 
                           <asp:DropDownList ID="CriticalBx" runat="server" AutoPostBack="True" 
                            Text='<%# Bind("BulletinType") %>' 
                               onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
                            <asp:ListItem>High</asp:ListItem>
                            <asp:ListItem>Critical</asp:ListItem>
                            <asp:ListItem>Change</asp:ListItem>
                           </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="style20">
                        TextSent:
                    </td>
                    <td>
                        DateTime:</td>
                </tr>
                <tr>
                    <td class="style20">
                        <asp:CheckBoxList ID="TextSentCheckList" runat="server" AutoPostBack="True" 
                            Text='<%# Bind("TextSent") %>'>
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:CheckBoxList>
                    </td>
                    <td>
                        <asp:TextBox ID="DateTimeTextBox" runat="server" 
                            Text='<%# Bind("DateTime") %>' />
                    </td>
                </tr>
            </table>
            <br />
            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Send" onclick="InsertButton_Click" />
            &nbsp;<asp:Button ID="InsertCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" 
                style="margin-bottom: 0px" />
        </InsertItemTemplate>

What I want to do is put in a IF statement for a drop down list where if a certain value is selected it does something.

However in the vb page it doesn't recognise the drop down list I have, however if I move the drop down list outside the template it does recognise it.

Does anyone know how I can do this but keep the drop down list in the template?

I hope this makes sense

Thanks

Recommended Answers

All 10 Replies

You might wanna have a look at the ClientID property.
ASP.NET adds the container ID to the ID of a control in the container but ClientID is always the ID you gave the control.

You might wanna have a look at the ClientID property.
ASP.NET adds the container ID to the ID of a control in the container but ClientID is always the ID you gave the control.

Sorry how do you mean? I have tried to reference the name of the template that all the controls are stored in if that is what you mean?

Other than that sorry I dont know what you mean :(

This is a quick and dirty way of doing it. I sometimes use it as a last resort.

Iterate through the Control collection of the page checking each controls ClientID.
If it matches the ID you gave the control, then you have a reference you can use.

Private classVariable_Control As Object = Nothing
Private Sub LookForControl(parent As Control)
	For Each ctl As Control In parent.Controls
		If TypeOf ctl Is DropDownList Then
			If ctl.ClientID.Equals("CriticalBx") Then
				classVariable_Control = ctl
				'Exit the Sub and do something with the object
			End If
		End If
		If ctl.HasControls Then LookForControl(ctl)
	Next
End Sub

But it just occurred to me.
Did you try the SelectedIndexChanged event?

Protected Sub CriticalBx_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CriticalBx.SelectedIndexChanged
		'Some event coding
End Sub

you need to do coding in cs file, you cannot add if statement in aspx page, .

something like,

if (dropdownlistbox1.selectedindex = 1) then
{
whateve u want}
else
{
}

Hope this helps.

you need to do coding in cs file, you cannot add if statement in aspx page, .

Not to complicate anything or something. But this is not entirely accurate.
You don't need to use CS or VB codebehind file.

You can use <script runat="server"></script> directly in the <head> section of the ASPX file, like in ASP. But that's another story and is quite messy.

<script runat="server">
Protected Sub SomeControl_SomeEvent(sender as Object, e As EventArgs) Handles SomeControl.SomeEvent
      If dropdownlistbox1.selectedindex = 1 Then
            'whatever u want
      Else
           'something else
       End If
End Sub
</script>
<script runat="server">
protected void SomeControl_SomeEvent(Object sender, EventArgs e)
{
      if (dropdownlistbox1.selectedindex = 1)
     {
            //whatever u want
      } else {
           //something else
       }
}
</script>

Hi I'm still not getting the CriticalBx being recognised in the Vb code, it keeps telling me I need to declare CriticalBx. It is very odd, its like the template is blocking it from being recognised.

Anymore thoughts would be appreciated

Ok. Here's another thought.
Can you see all controls in the template while in design mode?
If so, what happens if you double-click the dropdown control?

You should be "transferred" to the codebehind-file and a method is created for the dropdown-control's SelectedIndexChanged event.
And thus recognized.

On the other hand, have you checked to see if the dropdown gets declared while inside the template?
If it's not. Just manually declare it yourself in the codebehind-file. Protected WithEvents CriticalBx As DropDownList

Ok. Here's another thought.
Can you see all controls in the template while in design mode?
If so, what happens if you double-click the dropdown control?

You should be "transferred" to the codebehind-file and a method is created for the dropdown-control's SelectedIndexChanged event.
And thus recognized.

On the other hand, have you checked to see if the dropdown gets declared while inside the template?
If it's not. Just manually declare it yourself in the codebehind-file. Protected WithEvents CriticalBx As DropDownList

Hi thanks for your reply again. Well declaring the CriticalBx works in that it allows you to complete the IF statement but it isnt linking this to the CriticalBx on the web page. It almost see it as two different objects.

None of the controls in the insertTemplate are recognised in the VB code, is there some setting I need to change to make the controls visible or something?

Thanks

Hi thanks for your reply again. Well declaring the CriticalBx works in that it allows you to complete the IF statement but it isnt linking this to the CriticalBx on the web page. It almost see it as two different objects.

None of the controls in the insertTemplate are recognised in the VB code, is there some setting I need to change to make the controls visible or something?

Thanks

Hi I have managed to work out how to sort it

Dim CriticalList As DropDownList =  TryCast(FormView1.FindControl("CriticalBx"), DropDownList)

        If CriticalList.Text = ("Manchester") Then

            CiritcalList.Text = ("Employee1")

        End If

I found a example online which helped me understand how to reference the controls in the template, so thank you for all your assistance.

You're welcome.
It's great that you managed to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.