How to access a control in a web user control from its parent page

Yesterday I was answering some questions in ASP.NET forums. One of the questions I answered was “How can I access a TextBox control in a web user control from an / page”.

Today I decided to post this blog to explain how you can do it.

It’s like a piece of cake. I create a web user control named “WebUserControl.ascx” and add a TextBox to it like following:

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox runat="server" ID="TB_Test"></asp:TextBox>


Then, I create a web form named “Default/” and add my user control to it:

<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Default/.cs" Inherits="_Default" %>

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <asp:Button ID="Button1" runat="server" 
        onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

As you can see, I also added a button control that reads value of our TextBox in web user control and then write it to the page. Let’s see the event handler of Button1.Click:

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox t = (TextBox)WebUserControl1.FindControl("TB_Test");
        Response.Write(t.Text);
    }

The nice part is the use of FindControl method that gives a control that I’m looking for.

Finally, I just used a simple cast in order to convert the returned control from FindControl method. Because my control was a TextBox, I created a variable as a TextBox and then fill it with my casted control. When I cast it, I can access all properties and methods related to this control.

ASP.NET MVC beta has been released

This is a good news for everyone who’s working with ASP.NET MVC Framework that finally an official version of it has been released. ScottGu explained the new features very well that you can check out. Here is some new features in ASP.NET MVC that ScottGu described:

By the way, Don’t forget to download it by click here.

We have to thank all people who are working on this framework such as ScottGu, Phill Haack, Scott Hanselman, etc.

Nice job ASP.NET MVC team.

New beta version of Windows Live is now ready for download

Yesterday, while I was checking my emails, something attracted me suddenly: “Check out the beta” with a Windows Live Messenger’s logo. I clicked that link and it redirected my to Windows Live new download page. I downloaded the new beta version of messenger, toolbar and writer and after I check them out, I found out that this version of Windows Live is much better than previous versions even in beta version.

Messenger

There some interesting new features with the new version of Windows Live Messenger and I want to tell you some of the best  features:Photo taken by myself

· More customizable:
With this version of Windows Live Messenger, you can set your favorite picture as your messenger’s background picture. You can also change your contact look from small to large and more.

· Favorites:
In this version, you have a favorites section that you can drag your favorite contacts there so that you can access them much easier and also separately change the look of your favorite contacts.

· What’s new:
There is a “What’s New” section added to the version that keeps you posted about your contacts. For example, if one of your contacts added a new post to his/her space, Windows Live Messenger will notify you about that.

· SMS Sender:
With new Windows Live Messenger, it’s much easier to send SMS to your contact. You can send SMS to someone while you’re chatting with him/her.

Toolbar

As I checked out, there is no significant change in the new version of Windows Live Toolbar except that you can add your custom buttons to your toolbar. For example if you want to have Wikipedia to your toolbar you can add it using custom buttons.

Photo taken by myself

Writer

As you may know, Windows Live Writer is one the most popular services that enable you send your blogs much easier and better (As I’m doing right now).Photo taken by myself

· Compatible:
With Windows Live Writer you can post your blogs to Windows Live Spaces, Blogger, TypePad, WordPress, SharePoint and any website supports Windows Live Writer’s standards.

· Live Preview:
With the new version of Windows Live Writer, you can make the use of WYSIWYG. It means that you can have a live preview of your post in your website’s layout.

· Nice photo editor:
The new Windows Live Writer enables you to edit your blog photos straight in Writer not other photo editors such as Adobe Photoshop. It has some simple features such as Corp, etc. but so useful (I like it!).

The new version of Windows Live also has some new features like Movie Maker, Microsoft Office Outlook Connector and more that can be found at http://download.live.com .

The power of Visual Basic 2008 in XLINQ

Using LINQ to XML (XLINQ) will help you to access your XML data very easier, like never before. But I don’t know why, using XLINQ in Visual Basic is more interesting than C# for me.

In this post I want to show you how easy can be for VB to generate an XML file than C#.

Assume that we want to generate an XML file like following:

<?xml version="1.0" encoding="utf-8" ?>
<configs>
    <config name="Mohammad Mahdi Ramezanpour"></config>
</configs>

In order to generate such a thing in C#, you will need to write something like this:

XDocument xdoc = new XDocument();
XElement xroot = new XElement("configs");
XElement xitem = new XElement("config", 
                 new XAttribute("Name", "Mohammad Mahdi Ramezanpour"));
xroot.Add(xitem);
xdoc.Add(xroot);
xdoc.Save("C:\\sample.xml");

As you can see, I just created a new XDocument and then add some XElements to it. All things are very routine and then I saved the data into an XML file "C:\sample.xml".

Now, lets check such the code above in Visual Basic:

Dim xdoc As New XDocument
Dim xroot As XElement = <configs></configs>
Dim xitem As XElement = <config name="Mohammad Mahdi Ramezanpour"></config>
xroot.Add(xitem)
xdoc.Add(xroot)
xdoc.Save("C:\sample.xml")

in the code above I created an XML file exactly like our previous example and the result will be equal.

Lets take a look at this code snippet; In Visual Basic when you declare a variable as XElement, you can write your XML content exactly like the syntax you’re using in an XML file. It means that you can use XML tags (<configs></configs>) straight in your code.

As I said in my previous posts, I like VB more than C# and I thing VB is much powerful than C# at least in this part :).

My new website is now UP

As you know my previous domain stole from me by the company I bought the domain from. So I decided to register a new domain because I love blogging and my website is the only place I can share my knowledge with others.