Wednesday, October 12, 2005

How to call user controls value (like Textbox, button, checkbox etc.) in aspx page.

How to call user controls value (like Textbox, button, checkbox etc.) in aspx page.

When you use usercontrols in your form it’s very easy to get that user control value in your form.

I am taking one example you are creating one Usercontrol that have two textbox with User Id and Password.
And this usercontrols you can use in your form either Load that control run time or Drag that control in your form design time.

1) Create one usercontrol name: Test.ascx
That have one panel on design time name: Panel1
Call this method (AddControls) in Page_Load() event in your
UserContorl Page (Test.ascx)


public void AddControls()
{
Label lblUserName;
Label lblPassword;
TextBox txtUserName;
TextBox txtPassword;

lblUserName = new Label();
lblUserName.Text ="User Name : ";
txtUserName = new TextBox();
txtUserName.ID="txtUserId1";
lblPassword = new Label();
lblPassword.Text="Password : ";
txtPassword = new TextBox();
txtPassword.ID="txtPassword1";
txtPassword.TextMode =
System.Web.UI.WebControls.
TextBoxMode.Password;

Panel1.Controls.Add(lblUserName);
Panel1.Controls.Add(txtUserName);
Panel1.Controls.Add(lblPassword);
Panel1.Controls.Add(txtPassword);
}


2) Create one form name: TestUserControls.aspx now
There are two ways to use this usercontrol in your form(TestUserControls.aspx)

a) Drag this usercontrol in your form

<%@ Register TagPrefix="uc1" TagName="Test" Src="Test.ascx" %>


OR

b) Load this user control in Page_Load() event in your form
(TestUserControls.aspx) where you are using this control.

Load user control at Run time

private void Page_Load(object sender, System.EventArgs e)
{
private UserControl LevelControl;
LevelControl = (UserControl) LoadControl("Test.ascx");
LevelControl.ID =" MyUserControl";
LevelControl.EnableViewState = true;
Type LevelControlType = LevelControl.GetType();
Panel1.Controls.Add(LevelControl);
}

3) In your Form (TestUserControls.aspx) put one button name Button1
And print that user control textboxes value in one label control

This is the way to get UserContol value in your page.

private void Button1_Click(object sender, System.EventArgs e)
{
string userId;
string password;
userId = ((TextBox)Page.FindControl("MyUserControl")
.FindControl("txtUserId1")). Text.ToString();

password = ((TextBox)Page.FindControl("MyUserControl")
.FindControl("txtPassword1")).Text.ToString();

Label1.Text= "Welcome : " + userId + " Your Password id : " + password;
}


The Output will be :

"Welcome : ritesh your Password : kesharwani"


Ritesh Kumar Kesharwani
A D I T I , B A N G A L O R E
Software Professional
Cell : 91-9845657636
Page: http://www.riteshk.blogspot.com/

Tuesday, October 11, 2005

How to fire events in runtime generated controls in C#. NET

How to fire events in runtime generated controls in C#. NET

 

I am taking an example of Link button controls

 

Generate runtime Link button and after clicking each and every button some

Alert message should get fired. This can be done in simple 2 steps.

 

Client Side Events

 

1) Make one method that will generate runtime Link button

 

public void AddButton()

{

LinkButton lbnew;

      int i=1;

      while (i<5)

      {

        lbnew=new LinkButton();             

        lbnew.Text="Link" + i + "<br>" ;

        lbnew.ID="Lnk" + i;

        Panel.Controls.Add(lbnew);

        i++;

      }

}

 

2) Call AddButton() method in OnLoad Event

 

private void Page_Load(object sender, System.EventArgs e)

{

  AddButton();

  int i;

  for(i=1;i<5;i++)

  {

     ((LinkButton)Page.FindControl("Lnk"+i)).Attributes

.Add("Onclick","alert('you have clicked link - " + i + "')");

  }

 

Server Side Events

 

1) Make one method that will generate runtime Link button

 

public void AddButton()

{

LinkButton lbnew;

      int i=1;

      while (i<5)

      {

        lbnew=new LinkButton();             

        lbnew.Text="Link" + i + "<br>" ;

        lbnew.ID="Lnk" + i;

        lbnew.CommandArgument ="Lnk" +i;

      lbnew.Click += new

System.EventHandler(this.LinkButton_Click);     

Panel.Controls.Add(lbnew);

      i++;

      }

}

 

2) This events handler will call the LivkButton_click()

 

private void LinkButton_Click(Object sender,System.EventArgs e)

{

   Response.Write("you have clicked link button”);

}

 

Summary

 

Client side

When you generate Runtime controls you must give the Id of that controls

And need to be fire with Attribute properties of that control.

 

Server side

When you what to fire event in server side you need to add Event Handler on that link button control

 

 

 

 

 

 

 


Yahoo! Music Unlimited - Access over 1 million songs. Try it free.

Thursday, October 06, 2005

How to prevent Caching in ASP.NET

There are two ways to prevent cache in ASP.NET

1) Response.Cache.SetCacheability(HttpCacheability.NoCache)

And

2) Response.Cache.SetAllowResponseInBrowserHistory(False)

This code you can right in code behind on_Load events this will not allow user to cache the page or records.




Ritesh Kumar Kesharwani

A D I T I , B A N G A L O R E
Software Professional
Cell : 91-9845657636



Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.

Wednesday, October 05, 2005

Basic CURSOR example in SQL server

Basic CURSOR example in SQL server

 

Basic Syntax of Cursor

 

--Declare cursor in declare section

DECLARE CURSOR

--Open cursor for operation    

OPEN CURSOR

FETCH CURSOR

--Fetch record till end

WHILE  @@FETCH_STATUS =0

BEGIN

                                    USE CURSOR

END

--Close cursor

CLOSE CURSOR

--Remove from the memory

DEALLOCATE CURSOR

 

Different fetch statement

 

-- Fetch the last row in the cursor.

FETCH LAST FROM authors_cursor
-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM authors_cursor
-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM authors_cursor
-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM authors_cursor
-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM authors_cursor

Use Cursor to print database table records

 

IF EXISTS (SELECT name FROM sysobjects

      WHERE name = 'ReadData' AND type = 'P')

   DROP PROCEDURE ReadData

GO

 

CREATE PROCEDURE ReadData

AS

BEGIN

            DECLARE @STRINGNAME NVARCHAR (3999)

           

            DECLARE CUR_NAME CURSOR FOR

SELECT USERNAME FROM TBLUSERINFO

 

            OPEN CUR_NAME

                        FETCH NEXT FROM CUR_NAME INTO

@STRINGNAME

                        WHILE @@FETCH_STATUS =0      

           

            PRINT @STRINGNAME

                        BEGIN

                                    FETCH NEXT FROM CUR_NAME INTO

@STRINGNAME

            END

            CLOSE CUR_NAME

            DEALLOCATE CUR_NAME

END   

GO

 

Use Cursor to return Set of Data/Records

 

IF EXISTS (SELECT name FROM sysobjects

      WHERE name = 'ReturnDataSet' AND type = 'P')

   DROP PROCEDURE ReturnDataSet

GO

 

CREATE PROCEDURE ReturnDataSet

(

    @CUR_DATASET            CURSOR VARYING OUTPUT

)

AS

BEGIN

            SET @CUR_DATASET = CURSOR FOR

                        SELECT USERNAME FROM TBLUSERINFO

 

END   

GO

 

Check cursor status

 

IF EXISTS (SELECT name FROM sysobjects

      WHERE name = 'ReadCursor' AND type = 'P')

   DROP PROCEDURE ReadCursor

GO

 

CREATE PROCEDURE ReadCursor

(

@CUR_STATUS            NVARCHAR (200) OUTPUT,

@CUR_DATASET            CURSOR VARYING OUTPUT

)

AS

BEGIN

DECLARE @RETURN_CUR  CURSOR

EXEC ReturnDataSet @RETURN_CUR OUTPUT

           

IF CURSOR_STATUS ('VARIABLE','@RETURN_CUR') <= 0

            BEGIN

                        SET @CUR_STATUS =

'NO DATA AVAILABLE IN CURSOR.'           

            END

ELSE

            BEGIN

                        SET @CUR_DATASET = @RETURN_CUR

            END

 

END   

GO

 

 



Ritesh Kumar Kesharwani

A D I T I , B A N G A L O R E
Software Professional
Cell  : 91-9845657636


Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.