How to use "User Controls" in Web Form? [modified]
-
Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>
<table style="padding:0; border:solid 0px;">
<tr valign="middle">
<td style="vertical-align:top;border-bottom:solid 0px">
Keyword: <asp:TextBox ID="searchValue" runat="server" />
</td>
</tr>
</table>From my web form .aspx file, I wanted to access the control searchType this way:
<%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
<script runat="server">
protected void btnSearch_Click(object sender, EventArgs e)
{
TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
if (searchValue != null)
Response.Write(searchValue.Text);
else Response.Write("Bad request");
}But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai
Hai
modified on Friday, July 4, 2008 1:26 AM
-
Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>
<table style="padding:0; border:solid 0px;">
<tr valign="middle">
<td style="vertical-align:top;border-bottom:solid 0px">
Keyword: <asp:TextBox ID="searchValue" runat="server" />
</td>
</tr>
</table>From my web form .aspx file, I wanted to access the control searchType this way:
<%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
<script runat="server">
protected void btnSearch_Click(object sender, EventArgs e)
{
TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
if (searchValue != null)
Response.Write(searchValue.Text);
else Response.Write("Bad request");
}But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai
Hai
modified on Friday, July 4, 2008 1:26 AM
Did you add the control to the page by using something like below
nguyenhh wrote:
protected void btnSearch_Click(object sender, EventArgs e) { TextBox searchValue = (TextBox)Page.FindControl("searchValue"); Control searchType = Page.FindControl("searchType"); //never find this control if (searchValue != null) Response.Write(searchValue.Text); else Response.Write("Bad request"); }
This is not the way of using it. You should write some properties to your control, So that you can directly use your control properties. No need to find each control inside it.
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
-
Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>
<table style="padding:0; border:solid 0px;">
<tr valign="middle">
<td style="vertical-align:top;border-bottom:solid 0px">
Keyword: <asp:TextBox ID="searchValue" runat="server" />
</td>
</tr>
</table>From my web form .aspx file, I wanted to access the control searchType this way:
<%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
<script runat="server">
protected void btnSearch_Click(object sender, EventArgs e)
{
TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
if (searchValue != null)
Response.Write(searchValue.Text);
else Response.Write("Bad request");
}But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai
Hai
modified on Friday, July 4, 2008 1:26 AM
This is sample userctrl with textbox inside:
]]>
This is aspx page i include the user ctrl in this page, i use findcontrol method of usercontrol object, by using this u can find controls inside the usercontrols.
]]>
]]>protected void btn\_Click(object sender, EventArgs e) { ((TextBox)usrctrl1.FindControl("TextBox1")).Text = "adasd"; } Untitled Page
-
This is sample userctrl with textbox inside:
]]>
This is aspx page i include the user ctrl in this page, i use findcontrol method of usercontrol object, by using this u can find controls inside the usercontrols.
]]>
]]>protected void btn\_Click(object sender, EventArgs e) { ((TextBox)usrctrl1.FindControl("TextBox1")).Text = "adasd"; } Untitled Page
viivekk, Thank you for pointing that out, what I was missing is: instead of using:
TextBox searchValue = (TextBox)Page.FindControl("searchValue");
I should use:
TextBox searchValue = (TextBox)ControlFormBookSearch1.FindControl("searchValue");
as ControlFormBookSearch1 is the control id i used in my code. Thanks again.
Hai
-
Did you add the control to the page by using something like below
nguyenhh wrote:
protected void btnSearch_Click(object sender, EventArgs e) { TextBox searchValue = (TextBox)Page.FindControl("searchValue"); Control searchType = Page.FindControl("searchType"); //never find this control if (searchValue != null) Response.Write(searchValue.Text); else Response.Write("Bad request"); }
This is not the way of using it. You should write some properties to your control, So that you can directly use your control properties. No need to find each control inside it.
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
Sherin Iranimose, I am learning .net, could you give me some example code to do this the correct way. my web form file:
<%@ Import NameSpace="com.nangmoi" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Page Language="C#" MasterPageFile="~/MasterPageHome.master" Title="www.nangmoi.com" %><%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
<%@ Register src="Control/ControlFormBookSearch.ascx" tagname="ControlFormBookSearch" tagprefix="uc6" %><script runat="server">
private BookManager objManager = new BookManager();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack){GridView1.DataSource = objManager.getAuthorByAll(); GridView1.DataBind(); } } protected void btnSearch\_Click(object sender, EventArgs e) { //TextBox searchValue = (TextBox)Page.FindControl("searchValue"); TextBox searchValue = (TextBox)ControlFormBookSearch1.FindControl("searchValue"); if (searchValue != null) Response.Write(searchValue.Text); else Response.Write("Bad request"); }
</script>
<uc6:ControlFormBookSearch ID="ControlFormBookSearch1" runat="server" />user control page
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>
<table style="padding:0; border:solid 0px;">
<tr valign="middle">
<td style="vertical-align:top;border-bottom:solid 0px">
Keyword: <asp:TextBox ID="searchValue" runat="server" value="test" />
</td>
</tr>
</table>user control code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;public partial class Control_ControlFormBookSearch : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{} //How do i do this the correct way?? do i create properties like this? //then how do i use i