|
Note: The following is using javascript. You didn't specify if you were using client side events or server side events. The code behind code is VB.NET
The easiest way to do it is to add the "onclick" attribute to the button during Page_Load in the code behind.
Button1.Attributes.Add("onclick", "MoveFocus('" & userControl1.Controls("textbox1").clientID & "');")
What is happening here is that you will be sending the name of the textbox in the user control to the javascript function "MoveFocus". "MoveFocus" will be performed on the client-side when the button is clicked. "MoveFocus" should look something like this:
function MoveFocus(TextName)
{
var txt = document.getElementById(TextName);
txt.focus();
}
Hope that helps point you in the right direction.
Obligatory Disclaimer: Any code that appears may be untested. It is merely meant as a guide. If it doesn't work, don't blame me. Some parts of the job require independence. Or Google.
Obligatory Disclaimer: Any code that appears may be untested. It is merely meant as a guide. If it doesn't work, don't blame me. Some parts of the job require independence. Or Google.
Have faith in yourself; amateurs built the Ark, professionals built the Titanic.
|
|
|
|
|
Hello guys and thanks for your response. It seems like both of you guys are talking along the same lines but I still cant get this to work.
T7bros, as you mentioned above, I added the code Button1.Attributes.Add("onclick", "MoveFocus('" & userControl1.Controls("textbox1").clientID & "');") in the page's load event handler.
Here's what I've added:
addButton.Attributes.Add("onclick", "MoveFocus('" + pumpassembly.FindControl("_installationDateTextBox").ClientID + "');");
I'm adding the Javascript function as you menioned in the aspx page under script block:
:
function MoveFocus(TextName)
{
var txt = document.getElementById(TextName);
txt.Focus();
}
When I set a break point, it looks like its blowing up at ("_installationDateTextBox").ClientID meaning either its not finding this text box control on the user control or there's an issue with the client ID. Also _installationDateTextBox is the ID of that text box that needs to have the focus when the button gets clicked, they why are we doing .ClientID? Any ideas whats going on here?
Thanks
Tina
|
|
|
|
|
Tina P wrote: it looks like its blowing up at ("_installationDateTextBox").ClientID
Make sure that _installationDateTextBox exists, it should work, if you are getting an exception, it must be because no control has been found and nothing is being returned which doesnt have a clientid property
A most secure way to access controls inside a user control is to expose controls as properties
Just create a public readonly property that returns the textbox you want
Then, youll be able to reference that textbox like: pumpassembly.myTextbox.focus;
If you still get the same error (null reference), it could also be that the textbox hasnt been created yet and you might need to read a little about EnsureChildControls
Alexei Rodriguez
|
|
|
|
|
Hello there, Yes you are correct...it's throwing a NullReferenceException (pointing towards the clientID property). And to answer your question, yes "installationDateTextBox" (which is just the ID of the text box located in the ascx file) is found because when I try to see what's present in the myUserControl inside the debugger, I see all the controls that are embedded in myUserControl including the installationDateTextBox. But apparenetly it's not grabbing the clientID and therefore has Null in the paremeter list for MoveFocus function.
_
addButton.Attributes.Add("onclick", "MoveFocus('" + myUserControl.FindControl("_installationDateTextBox").ClientID + "')");
What am I doing wrong here you guys???
Thanks
Tina
|
|
|
|
|
Tina P wrote: And to answer your question, yes "installationDateTextBox" (which is just the ID of the text box located in the ascx file)
Whats the name of the control, installationDateTextBox or _installationDateTextBox??
Post the sourcecode of your ascx file
Tina P wrote: myUserControl.FindControl("_installationDateTextBox")
This must return a control, and all controls have a clientid property, the exception must be because the control is not found and not because of the clientid property
Alexei Rodriguez
|
|
|
|
|
Hey Alexei, thanks for the response.
The answer to the question: Whats the name of the control, installationDateTextBox or _installationDateTextBox??" is:
- Assuming that by the name of the control, you are actually referring to the ID of the text box control, Yes it is "_installationDateTextBox" as shown below.
<asp:textbox id="_installationDateTextBox" runat="server" width="91px" xmlns:asp="#unknown"></asp:textbox>
As far as the code behind of this ascx file, all I'm doing there is setting the text property of this control.
_installationDateTextBox.Text = DataAccessLayer.DataSource.getReaderByIdForInstall(assemblyID);
The assemblyId is being retrieved from the database on the apsx page itself, so I'm passing it to the control by using a public property.
The weird thing is that when I try to find a control which is located on the page itself (aspx file) by doing something like the following:
Control temp = FindControl("InfoLinkButton");
It works fine, you can see the temp variable here with the actual LinkButton control and all its properties in the debugger. But when I replace the InfoLinkButton control with _installationDateTextBox (which is located on the ascx file) then the temp would have Null, meaning its not found??? That's just bizarre! Here's the code:
Control temp1 = myUserControl.FindControl("_installationDateTextBox"); (temp1 is Null when this line execute)
What am I doing wrong here
Tina
|
|
|
|
|
Expose your control as a public property for your usercontrol
Also, paste the sourcecode of your usercontrol
Alexei Rodriguez
|
|
|
|
|
public partial class UserControls_PumpAssembly : System.Web.UI.UserControl
{
int assemblyID;
public void Assemblyid(int val)
{
assemblyID = val;
}
protected void Page_Load(object sender, EventArgs e)
{
_modelTextbox.Text = DataAccessLayer.AssemblyDataSource.getSensorModelbyUsingAssemblyID(assemblyID);
_serialNumTextBox.Text = DataAccessLayer.AssemblyDataSource.getSensorSerialNumberbyUsingAssemblyID(assemblyID);
_installationDateTextBox.Text = DataAccessLayer.AssemblyDataSource.getReaderByAssemblyIdForInstall(assemblyID);
}
protected void gasSeparatorddl_OnselectedIndexChanged(object sender, EventArgs e)
{
DataAccessLayer.AssemblyDataSource.InsertGasSeparationAbility(assemblyID);
}
This is pretty much all the code I have in the code behind of the user control. As you can see I'm just setting the text properties of a few controls including the installtiondate.
Can you show me using an example what exactly are you suggesting as far as exposing them as properties please?
Thanks sooo much.
Tina
|
|
|
|
|
Code to expose your textbox as a property an just call it from the page where the control is like myUserControl.installationDateTextBox
public TextBox installationDateTextBox
{
get { return this._installationDateTextBox; }
}
Tina P wrote: protected void Page_Load(object sender, EventArgs e) { _modelTextbox.Text = DataAccessLayer.AssemblyDataSource.getSensorModelbyUsingAssemblyID(assemblyID); _serialNumTextBox.Text = DataAccessLayer.AssemblyDataSource.getSensorSerialNumberbyUsingAssemblyID(assemblyID); _installationDateTextBox.Text = DataAccessLayer.AssemblyDataSource.getReaderByAssemblyIdForInstall(assemblyID); }
Unless you have a reason to read this from the DB each time, you should include that code inside a page.ispostback condition
In my previous post, I meant your source code (.ascx) and not the code behind (.ascx.cs)
Alexei Rodriguez
modified on Saturday, September 27, 2008 11:01 PM
|
|
|
|
|
Hello there,
Thanks for the example...i tried to use the read only property as you mentioned above and accessed the text box from the page's code behind in this way TextBox textBx = pumpassembly.installationDateTextBox;
technically it should have the text box control in the textBx variable when I step through this code, but it returns NULL just as it was before when I was using FindControl approach????
By the way, just an FYI, I'm loading these user controls in the pre_render event...so they don't get loaded in the page load...anyways, but the code TextBox textBx = pumpassembly.installationDateTextBox; is being executed from a button called ADD on the page (Add buttons click event handler). The reason I'm mentioning this is because all of a sudden it occurred to me that what if the user control (whose text box we are trying to access) is not loaded at the time, but that's not true because the page has already been loaded at this time once and that's when user clicks the Add button. I'm sooooo confused
Also sorry i must have misunderstood, here is the ascx code you asked for.
<div style="margin-bottom: inherit; bottom:inherit; margin-top: inherit; position: relative; display: block;<br mode=" hold=" /> float: left; border-style:None; width: 600px; height:300px">
<div style="position: relative; float: left;">
<asp:label id="pumpAssemblyLabel" runat="server" font-bold="true" text="General Information" xmlns:asp="#unknown"></asp:label> </div>
<asp:label id="_installationDateLabel" runat="server" text="Installation Date" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="_installationDateTextBox" runat="server" width="91px" xmlns:asp="#unknown"></asp:textbox>
<asp:label id="_manufacturerLabel" runat="server" text="Manufacturer" xmlns:asp="#unknown"></asp:label>
<asp:dropdownlist id="_manufacturerDropdown" runat="server" appenddatabounditems="True" xmlns:asp="#unknown">
DataSourceID="Manufacturerds" DataTextField="ManufacturerName" DataValueField="ManufacturerID">
<asp:listitem>---Select One---</asp:listitem>
</asp:dropdownlist>
<asp:objectdatasource id="Manufacturerds" runat="server" typename="DataAccessLayer.ManufacturerDataSource" xmlns:asp="#unknown">
SelectMethod="GetManufacturerUsingDB"></asp:objectdatasource>
<br />
<asp:label id="_modellabel" runat="server" text="Model" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="_modelTextbox" runat="server" width="111px" xmlns:asp="#unknown"></asp:textbox>
<asp:label id="_serialNumLabel" runat="server" text="Serial Number" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="_serialNumTextBox" runat="Server" width="113px" xmlns:asp="#unknown"></asp:textbox>
<br /></div>
Thanks
Tina
|
|
|
|
|
Tina P wrote: I'm loading these user controls in the pre_render event...so they don't get loaded in the page load
What do you mean by this??
Prerender is of the last events in the page, after handling any licks, selectedindexchanged and so on
Anyways, i dont know what might be causing the problem, i made a really basic example
UserControl:
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Test_WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Page:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.aspx.cs" Inherits="Test_WebUserControl" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Test_WebUserControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox txt = this.WebUserControl1.FindControl("TextBox1") as TextBox;
txt.Text = "Something";
}
protected void Page_Prerender(object sebder, EventArgs e)
{
//TextBox txt = this.WebUserControl1.FindControl("TextBox1") as TextBox;
//txt.Text = "Something";
}
}
I tryed setting the text property in the prerender and it also worked
Alexei Rodriguez
|
|
|
|
|
hey guys, I have a drop down list which is supposed to show a combination of two dates (lets say insert date and pull date) separated by a underscore or something. The problem is that I only want the date to be shown and not the time (it's putting in a default time 12 AM in there) Now I could call ToShortDateString on the date that is being returned if I only had a single date coming back but since I'm getting a SQLDataReader object back, I can't even call that on it.
here is the code that I'm using:
public static SqlDataReader getDatesByWellId(Int32 wellId)
{
string dsn = System.Configuration.ConfigurationManager.ConnectionStrings["AMasterConnectionString1"].ConnectionString;
SqlConnection conn = new SqlConnection(dsn);
String query = "Select Convert(Varchar,InstallDate)+' '+Convert(Varchar,PullDate) As DateInfo From tblAssemblyMaster With (NoLock) Where WellId=" + wellId + " " + "Order By InstallDate Asc";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
return sdr;
}
Is there anything that I can do to fix this? Like I said earlier all I wanna display is a date range for instance something like this: 4/11/2007 - 6/19/2008. Can someone shed some light on this!
Thanks
Tina
|
|
|
|
|
Select Convert(Varchar,InstallDate,105)+' - '+Convert(Varchar,PullDate,105) As DateInfo ...etc
|
|
|
|
|
Thanks for your help, I just started working with .Net and didn't even know about those formatting values. I appreciate your help!
Tina
|
|
|
|
|
Dear All,
I am using C# , Asp.net2.0
I am using server.tranfer to redirect the another webpage with parameter
I have root folder and my aspx file is inside another folder in root that is root -> folder -> test.aspx
Now I am using following code redirect as
Server.Transfer("/Demo/FrmChecklist.aspx", true);
But I am getting error as path not mapping
Please suggest
imran khan
|
|
|
|
|
Try
Server.Transfer("~/Demo/FrmChecklist.aspx", true);
|
|
|
|
|
Hi,
I have a checkboxlist which i pull the options from a database on the page and I have got it submitting multiple checked values into the database, but im having problems creating the page to display which values have been checked. Could someone please help?
The look up table is:
Table - refCommittee
CommitteId - int
CommitteeName - varchar
Active - bit
The table holding inserted values
Table - tblApprovingCommGroup
ID - int
PolicyID - int
CommitteID - int
Many thanks
|
|
|
|
|
i want to display the file name not whole path in asp.net C#.
I use datalist and in datalist i have taken link button to show the file name. my path storedin database.
thanks
|
|
|
|
|
You're not having much luck with this, are you?
to extract filename.ext out of str = "drive/folder/path/filename.ext" try
str.SubString(str.LastIndexOf("/") + 1)
|
|
|
|
|
He's already been told the right way numerous times
string str = "drive/folder/path/filename.ext";
string filename = Path.GetFilename(str);
|
|
|
|
|
J4amieC wrote: string filename = Path.GetFilename(str);
Yes, that works too! Do you know, I never thought ot try that on a simple string - just assumed the argument needed to have been defined through a file object (or HttpPostedFile or something)... how embarrassing
|
|
|
|
|
Hi All
I am using Replace in Old ASP
and its not working
I am using
Replace cont,"my_documents/","master/my_documents/"
and It not replace the text
any body have solution
Amit Battan Ror
battan20@gmail.com
|
|
|
|
|
Probably because you haven't take case-sensitivity into account
clickety[^]
|
|
|
|
|
hi i am using file up loader to store path of the file in database and value show in datagrid in linkbutton text i want when i click on this link file should be open. if file is .rar or .zip than open save dilouge box .
I will do my best?
Integrated Solutions, Bikaner (Raj.), India
|
|
|
|
|
Are you facing any issues in this? What's your requirement?
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|