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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a control within a usercontrol on a webform 1

Status
Not open for further replies.

nimarii

MIS
Jan 26, 2004
213
US
Hello,

I have a webform that has usercontrol (DateTimeSpan), and a "View Report" button.

What I need to do, is when the button is clicked, I need to ensure that all the fields on the DateTimeSpan usercontrol are populated before continuing.

How can I reference the controls within a usercontrol from the button's onclick event in webform.aspx.cs?

Thanks!
 
the ideal to do this u have to create "Properties" and methods in the ASCX.

another approach is to access the Controls collection of the ASCX object...

Known is handfull, Unknown is worldfull
 
how do i access the controls collection of the ASCX object?
 
sample code:

ASCX CODE
Code:
<%@ Control Language="VB" %>
<input id="TheTextField" onclick="Clicked" type="text" runat="server">
<asp:Label id="Testing" runat="server"></asp:Label>

ASPX CODE
Code:
<%@ Page Language="VB" %>
<%@ Register TagPrefix="Acme" TagName="Address" Src="test.ascx" %>
<script runat="server">
	sub page_load
		Dim Ctrl as Control
		for each Ctrl in ASD.Controls
			response.write(Ctrl.Id & "<br>")
		next
	end sub
</script>
<html>
<head>
</head>
<body>
    <form method="post" runat="server">
        <Acme:Address id="ASD" runat="Server"/>
    </form>
</body>
</html>

Known is handfull, Unknown is worldfull
 
thanks for the sample code, but I'm actually trying to access the controls of the ascx from the code-behind file, the webform.aspx.cs file....

here's the pseudo code of what I'm trying to achieve:
Code:
private void Button_Click(object sender, System.EventArgs e)
		{
if(usercontrol.datetextbox.value != null) then
			Label3.Enabled = true;
			DropDownList2.Enabled = true;
			ViewReports.Enabled = true;
			DropDownList3.Enabled = true;
		}

am i taking the wrong approach to this?
 
the code i gave will also work with with CodeBehind:

>>usercontrol.datetextbox

must be
usercontrol.Controls(DateTextBoxIndexNumber)

DateTextBoxIndexNumber = Position of the textbox (ie first control or second or third etc...)


Known is handfull, Unknown is worldfull
 
thanks vbkris, but I'm getting the following error now when I compile:
Code:
The type or namespace name 'DateTimeSpan1' could not be found (are you missing a using directive or an assembly reference?)

DateTimeSpan1 is the id of the usercontrol....i dont know how to reference the ascx file in my code-behind file...it looks like i need to declare it somehow?
 
hmm, that could be a problem.

try this:

Dim MyUserControl=FindControl("UserControlName")
response.write(MyUserControl.Conrols(0).id) 'assuming the field that u r trying to access s the first field in the ASCX file. u could also use the FindControl method of the MyUserControl file.


dim TheTextField=MyControl.FindControl("FieldID")
response.write(TheTextField.value)


Known is handfull, Unknown is worldfull
 
did that work???

Known is handfull, Unknown is worldfull
 
i'm still working it out, i think maybe a few things were lost in translation to c#...

i think i'm getting close, will let know, thanks!!!!
 
ok, so after tooling around with trying to declare the variable as an object, and then trying to pull a string value from it, etc etc, and getting nowhere, i decided to take a different approach.

thanks so much for your attention on this one vbkris, i genuinely appreciate it.

i instead used the validationsummary object in the ascx page, and used the "causesvalidation" property for the button on the aspx page....

phew.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top