Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access user control elements from code behind? 1

Status
Not open for further replies.

sarahnice

Technical User
Nov 18, 2002
101
IE
Hi all,

I am having major difficulty with the following. I have an aspx page with a reference to an ascx user control. The code for the control is as follows:

Code:
<%@ Control Language="vb" Inherits="myUserControl" Src="../../codeBehind/test.aspx.vb" %>

<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %>
<MM:DataSet 
  id="FI_OpinionPoll"
  runat="Server"
  IsStoredProcedure="false"
  ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_FI_DBConn") %>'
  DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_FI_DBConn") %>'
  CommandText='<%# "SELECT FI_Poll.FI_PollId, FI_Poll.FI_PollTitle, FI_Poll.FI_TotalVotes, FI_Poll.FI_NumPollOptions, FI_PollOptions.FI_OptionText, FI_PollOptions.FI_OptionVotes, FI_PollOptions.FI_PollId_Fk, FI_PollOptions.FI_PollOptionId  FROM FI_Poll, FI_PollOptions  WHERE FI_PollOptions.FI_PollId_Fk = (SELECT MAX(FI_Poll.FI_PollId) FROM FI_Poll)" %>'
  Debug="true">
</MM:DataSet>

<MM:PageBind runat="server" PostBackBind="true" />

<table width="200" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <div class="panel" id="poll_input" style="display: block;">
        <table width="99%" border="0" cellpadding="0" cellspacing="0" class="right_frame_userlogin">
          <tr>
            <td colspan="3" align="center">
              <strong><%# FI_OpinionPoll.FieldValue("FI_PollTitle", Container) %></strong>
              <asp:Label ID="lblOpinionId" runat="server" Visible="false" Text='<%# FI_OpinionPoll.FieldValue("FI_PollId", Container) %>'></asp:Label>
            </td>
          </tr>
	  <tr>
	    <td height="4" colspan="3"></td>
	  </tr>
          <tr>
	    <td width="2%"></td>
	    <td></td>
	    <td width="2%"></td>			  
	  </tr>
	  <tr>
	    <td width="2%"></td>
	    <td width="80%">
	      <asp:RadioButtonList ID="aaa" CellPadding="0" CellSpacing="0" CssClass="right_frame_table" DataSource="<%# FI_OpinionPoll.DefaultView %>" DataTextField="FI_OptionText" DataValueField="FI_PollOptionId" runat="server" RepeatDirection="Vertical" RepeatLayout="table" TextAlign="right" OnSelectedIndexChanged="changed" AutoPostBack="true"></asp:RadioButtonList>
            </td>
	    <td width="2%"></td>
	  </tr>
	  <tr>
	    <td colspan="3" height="4"></td>
	  </tr>		
	  <tr>
	    <td height="4" colspan="3"></td>
	  </tr>
	  <tr>
	    <td colspan="3">
	      <asp:Button ID="btnVote" runat="server" Text="Vote" OnClick="vote_Click" />
	    </td>
	  </tr>
	  <tr>
	    <td height="4" colspan="3"></td>
	  </tr>
        </table>
      </div>
    </td>
  </tr>
</table>

The code behind file test.ascx.vb is as follows:

Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.OleDB

Public Class myUserControl : Inherits UserControl

	Protected btnVote As Button
	Protected aaa As RadioButtonList
	Protected lblOpinionId as Label
	
	Public Sub vote_Click(sender As Object, e As EventArgs)
		Dim optionSelected As String

		btnVote.Text = "test"
	End Sub
	
	Public Sub changed(sender As Object, e As EventArgs)
		btnVote.Text = aaa.SelectedItem.Text
	End Sub
End Class

I can get/set the text on the button btnVote but I am unable to access the RadioButtonList. I cannot get the SelectedItem value. I am getting Object reference not set errors.

I have tried many different suggestions I found on other sites but nothing seems to work.

Any advice would be greatly appreciated.

Thanks :)
 
You need to make the control Public so it can be accessed.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I assume you mean that I have to change the declaration of the variable aaa in the code behind from protected to public. After making the change, I still get an Object reference not set error.
 
Have you added the correct declaration for the control (see thread855-1075687 as this is the same problem)?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I am not trying to access the user control from the aspx file. I am only trying to access the web controls on the ascx control from the ascx code behind file.

I have been playing around with this and I can set/get the properties of the Labl and Button on the ascx file but the problem arises when I try to get/set the properties of the RadioButtonList.
 
I think I may have figured this one out. I changed the code to display the SelectedIndex of the RadioButtonList but it returns -1 every time. I think the problem is that the form is submitted each time an option in the radio list is selected. When the user control reloads, the radioButtonList is reloaded with the data from the dataset.

I need to only load the dataset the first time the page loads. Do I have to do this programatically?
 
Apologies if this should go into a seperate thread.

The reason for this user control is to implement an opinion poll on my sample site. I want the RadioButtonList to give a list of options, which are populated from the dataset. When the user selects an option and clicks on the vote button, the display of the user control should change to no longer show the options but to show the results of the poll so far.

I am pretty new to ASP.NET and I am doing this project so that I can try to learn .NET. This user control is the first one I have tried that has some dynamic properties to it. If someone could point me to a very good site that would help or anyone could give me some pointers, I would be really grateful. I think that once I can get one user control working, then I can use that for the basis of any future ones.

Thanks :)
 
When the user selects an option and clicks on the vote button, the display of the user control should change to no longer show the options but to show the results of the poll so far.
In that case you should be able to use Page.IsPostback to determine when to show the poll or the results.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for the help. I'm starting to get something resembling what i'm looking for now.
 
No problem. Here's some articles on User Controls that may give you some guidance:




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top