link button firing a stored procedure

R

Rod Snyder

I'm trying to set up a page with an asp.net link button that would send a
user to a certain page and on page load execute a specific stored procedure
tied to the button on the previous page. The link would be in a nav area and
be something like "update". It would send the user to the main page and
execute the stored procedure associated with the update functionality. I'm
new to this area of .NET and would appreciate any suggestions, directions or
comments.

Rod
 
S

Showjumper

Assuming i understand you correctly, you can accomplish your task in a
couple of ways. One, you can add a sub to the page that has the linkbutton
and this sub would file via the onclick event of the linkbutton. The sub
would redirect the user to the new page and in the page load of the new
page your update code would fire. If you need to pass a parameter, you can
pass one by adding the paramater to the context object of the 1st page:
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")
And then get it on the new page via Dim x as Integer =
CType(Context.Items("myParameter"),Integer)
Note i copied this code from the post below yours. Alternatively you can
have the update routine fire onclick via the linkbutton on the first page
then redirect to the second page.
 
S

Steven Cheng[MSFT]

Hi Rod,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you are dealing with an ASP.NET web page and you'd
like to navigator to another page when a certain LinkButton on the page is
clicked and then do some data manipulations(call a store procedure) on the
new web page. Yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, here is my suggestion:
In ASP.NET webpage, if you want to direct to another new page, you can use
either the "Server.Transfer" or "Response.Redirect" method. The difference
between them is Response.Redirect won't take any request area form datas to
the new page while the Server.Transfer can remain the former request datas.
As Showjumper has mentioned, if you need to pass some parameters to the new
page, you'd better use the "Server.Transfer".
For detailed infos on the two method, you may view the following reference
in MSDN:
#Server.Transfer
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/iis/
ref_vbom_seromtr.asp

#Redirecting Requests in IIS
http://msdn.microsoft.com/library/en-us/iissdk/iis/configuring_iis_to_redire
ct_requests.asp?frame=true

Also, I've made a couple of simple test pages to show how to redirect a
page(using the LinkButton as example):
---------------------------First.aspx page
source----------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>First</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
Do Update:<asp:LinkButton id="lnkUpdate" runat="server"
Width="40px">Update</asp:LinkButton>
<asp:TextBox id="txtName" runat="server"></asp:TextBox></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------------First.aspx code-behind class
source----------------------
public class First : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.LinkButton lnkUpdate;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lnkUpdate.Click += new System.EventHandler(this.lnkUpdate_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void lnkUpdate_Click(object sender, System.EventArgs e)
{
Context.Items.Add("name",txtName.Text);
Server.Transfer("Next.aspx");
}
}

-----------------------------------------Next.aspx page
source-----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Next</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="ËÎÌå"></FONT>
</form>
</body>
</HTML>


-------------------------------------Next.aspx code-behind page class
----------------------------------
public class Next : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string name = Context.Items["name"].ToString();
Response.Write("<br>Hello " + name + "!");

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}

---------------------------------------------------------------------


In addition, as for the data manipulation in ASP.NET such as calling stored
procedure.. here is a good tech ariticle in MSDN, I think it'll be also
helpful to you:

#Jumping into ASP.NET Part 2: Creating the Data Storage Layer with SQL
Server 2000
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/aspnet-jumpinto-part2.asp

Please try out the preceding suggestion to see whether they're helpful. If
you need any further assistance, please feel free to let me know.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

Rod Snyder

Showjumper:
Just to clarify, let's say I have a "main" page that has several links,
New, Update, Edit, etc. This navigation would be on each page of the web
app. From the Main page, the person goes to Update on the update page the
link to New would need to send the user back to Main and execute the New
stored procedure that would return a list of all of the "New" activities the
person can do. So, the New link on Main would need to be a link button whose
on click event would fire the stored procedure. The Main page would also
need a sub on its page load event to which the "New" parameter would be
passed when the link (on the Update page sending the person back to Main) is
clicked.

The New parameter would be added to the context object of Main (along with
the parameters of any of the other categories (Update, View, etc) that could
be called on page load from a link within the application.

Am I close?

Rod
 
S

Steven Cheng[MSFT]

Hi Rod,


Thanks for your followup. I think the routine you described in the last
reply is correct and could be a means to implement what you want. However,
I've another means if your page is not very complex. I think you may try
put those things all in the same page and when different linkbutton such as
(new, delete or update) is clicked , the page display the related part and
hide the others. In ASP.NET web page , you can use the Panel control to
contains other sub controls and you're able to show or hide a control
freely. Thus, as for the problem you mentioned, I think you can provide two
panels one contains the controls and elements which is to be shown at "new"
situation, and the other panel contains the thing which is to be shown at
"update" situation. And when "New" link is clicked, you show the "new"
panel and hide the "update" panel. Also, the same when "update" panel is
clicked. If so, you may avoid using multi pages or transfer parameters
between different pages.

To make this clearly, I've made a simple sample page , you may have a check
to see whether this is suitable for your situation:

---------------------------------------aspx
file------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MultiContent</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="600" align="center">
<tr>
<td colspan="2">
<asp:Label id="lblMessage" runat="server"></asp:Label><INPUT
id="hdState" type="hidden" runat="server">
</td>
</tr>
<tr>
<td colspan="2">
<table width="100%" align="center">
<tr>
<td><asp:LinkButton id="lnkMain"
runat="server">Main</asp:LinkButton></td>
<td><asp:LinkButton id="lnkNew"
runat="server">New</asp:LinkButton></td>
<td><asp:LinkButton id="lnkUpdate"
runat="server">Update</asp:LinkButton></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:panel id="pnlNew" runat="server">
<TABLE width="100%" align="center">
<TR>
<TD colSpan="2">This is the New area</TD>
</TR>
<TR>
<TD>
<asp:Label id="Label1" runat="server">NewName</asp:Label></TD>
<TD>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label2" runat="server">NewPassword</asp:Label>
</TD>
<TD>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Create"></asp:Button>
</TD>
</TR>
</TABLE>
</asp:panel>
</td>
</tr>
<tr>
<td>
<asp:panel id="pnlUpdate" runat="server">
<TABLE width="100%" align="center">
<TR>
<TD colSpan="2">This is the Update area</TD>
</TR>
<TR>
<TD>
<asp:Label id="Label3" runat="server">EditName</asp:Label>
</TD>
<TD>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
</TD>
</TR>
<TR>
<TD>
<asp:Label id="Label4" runat="server">EditPassword</asp:Label>
</TD>
<TD>
<asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
<asp:Button id="Button2" runat="server"
Text="Update"></asp:Button>
</TD>
</TR>
</TABLE>
</asp:panel>
</td>
</tr>
</table>
</form>
</body>
</HTML>


----------------------------------------------------------code behind page
class --------------------------------------------------
public class MultiContent : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel pnlNew;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.LinkButton lnkMain;
protected System.Web.UI.WebControls.LinkButton lnkNew;
protected System.Web.UI.WebControls.LinkButton lnkUpdate;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdState;
protected System.Web.UI.WebControls.Panel pnlUpdate;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
Do_Main();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lnkMain.Click += new System.EventHandler(this.lnkMain_Click);
this.lnkNew.Click += new System.EventHandler(this.lnkNew_Click);
this.lnkUpdate.Click += new System.EventHandler(this.lnkUpdate_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void lnkMain_Click(object sender, System.EventArgs e)
{
if(!hdState.Value.Equals("main"))
{
Do_Main();
}
}

private void lnkNew_Click(object sender, System.EventArgs e)
{
Do_New();
}

private void lnkUpdate_Click(object sender, System.EventArgs e)
{
Do_Update();
}


private void Do_Main()
{
pnlNew.Visible = false;
pnlUpdate.Visible = false;
hdState.Value = "main";
lblMessage.Text = "Current state is Main!";
}

private void Do_New()
{
pnlNew.Visible = true;
pnlUpdate.Visible = false;
hdState.Value = "new";
lblMessage.Text = "Current state is New!";
}

private void Do_Update()
{
pnlNew.Visible = false;
pnlUpdate.Visible = true;
hdState.Value = "update";
lblMessage.Text = "Current state is Update!";
}

}


--------------------------------------------------
Also, if you have any questions , please feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

Rod Snyder

Steve:
This sounds really cool. Let me put together your pages and take a look.
This might be a perfect solution. I'll let you know.

Rod
 
S

Steven Cheng[MSFT]

Hi Rod,


Have you had a chance to try my suggestion or have you resolved your
problem? Please check out my last reply and if you have any questions,
please feel free to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top