动态LinkBut​​ton [英] Dynamic LinkButton

查看:73
本文介绍了动态LinkBut​​ton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在购物车应用程序中,假设用户已经下了5个订单,我想

向他展示5个LinkBut​​tons(每个订单一个),这样当他点击
$ b时$ b首先是LinkBut​​ton,他会看到他的第一个订单的详细信息。

同样,如果他点击第二个LinkBut​​ton,他将会看到第二个订单的

细节他放了。 LinkBut​​tons的文本

将是1 2 3等。所以这个用户会看到1 2 3 4 5作为

LinkBut​​tons。


问题是如何动态添加LinkBut​​tons。我是否必须首先创建一个用户控件&
然后使用Page.LoadControl将

LinkBut​​tons添加到ASPX页面?或者还有其他方法吗?


有人可以建议我如何去做吗?

In a shopping cart app, suppose a user has placed 5 orders, I want to
show him 5 LinkButtons (one for each order) so that when he clicks the
first LinkButton, he would be shown the details of his first order.
Likewise if he clicks the second LinkButton, he will be shown the
details of the second order he had placed. The Text of the LinkButtons
will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the
LinkButtons.

The problem is how do I add the LinkButtons dynamically. Do I have to
first create a user control & then using Page.LoadControl add the
LinkButtons to the ASPX page? Or is there some other way out?

Can someone suggest me how do I go about it?

推荐答案

使用New运算符创建一个链接按钮。设置其所有属性。将

添加到页面的控件集合中。


如果您需要更好地控制按钮的显示位置(我这是$

怀疑你这样做了,把它们添加到你放在特定位置的面板上。


< rn ** @ rediffmail.comwrote in message

news:11 ********************* @ k70g2000cwa.googlegro ups.com ...
Create a link button using the New operator. Set all of its properties. Add
it to the page''s Controls collection.

If you need finer control over where the buttons will appear (which I
suspect you do), add them to a panel you have placed in a particular spot.

<rn**@rediffmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

在购物车应用程序中,假设用户已经下了5个订单,我想

向他展示5个LinkBut​​tons(每个订单一个),这样当他点击
$ b $时b首先是LinkBut​​ton,他会看到他的第一个订单的详细信息。

同样,如果他点击第二个LinkBut​​ton,他将会看到第二个订单的

细节他放了。 LinkBut​​tons的文本

将是1 2 3等。所以这个用户会看到1 2 3 4 5作为

LinkBut​​tons。


问题是如何动态添加LinkBut​​tons。我是否必须首先创建一个用户控件&
然后使用Page.LoadControl将

LinkBut​​tons添加到ASPX页面?或者还有其他方法吗?


有人可以建议我怎么做吗?
In a shopping cart app, suppose a user has placed 5 orders, I want to
show him 5 LinkButtons (one for each order) so that when he clicks the
first LinkButton, he would be shown the details of his first order.
Likewise if he clicks the second LinkButton, he will be shown the
details of the second order he had placed. The Text of the LinkButtons
will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the
LinkButtons.

The problem is how do I add the LinkButtons dynamically. Do I have to
first create a user control & then using Page.LoadControl add the
LinkButtons to the ASPX page? Or is there some other way out?

Can someone suggest me how do I go about it?


< rn ** @ rediffmail.com写信息

news:11 ********************* @ k70g2000cwa。 googlegro ups.com ...
<rn**@rediffmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

在购物车应用中,假设用户已经下了5个订单,我想

给他看5个LinkBut​​tons(每个订单一个),这样当他点击第一个LinkBut​​ton

时,他会看到他第一个订单的详细信息。

同样如果他点击第二个订单LinkBut​​ton,他将会看到他所放置的第二个订单的

细节。 LinkBut​​tons的文本

将是1 2 3等。所以这个用户会看到1 2 3 4 5作为

LinkBut​​tons。


问题是如何动态添加LinkBut​​tons。我是否必须首先创建一个用户控件&
然后使用Page.LoadControl将

LinkBut​​tons添加到ASPX页面?或者还有其他出路吗?


有人可以建议我如何去做吗?
In a shopping cart app, suppose a user has placed 5 orders, I want to
show him 5 LinkButtons (one for each order) so that when he clicks the
first LinkButton, he would be shown the details of his first order.
Likewise if he clicks the second LinkButton, he will be shown the
details of the second order he had placed. The Text of the LinkButtons
will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the
LinkButtons.

The problem is how do I add the LinkButtons dynamically. Do I have to
first create a user control & then using Page.LoadControl add the
LinkButtons to the ASPX page? Or is there some other way out?

Can someone suggest me how do I go about it?



很容易死。


当你的页面加载时,我假设你已经知道有多少订单<购物车中的
- 可能你将购物车存放在一个Session变量中,或者是从一个数据库或其他任何东西中取出它来获取它 - 并不重要......

以下假设您已将其取入


1)在Page_Init中,而不是Page_Load,取出您的购物车


2)对于购物车中的每个元素(订单,产品等),创建一个新的

LinkBut​​ton()对象:

LinkBut​​ton lnkDynamic ;


foreach(< whateverin< cart>)

{

lnkDynamic = new LinkBut​​ton();

lnkDynamic.EnableViewState = true;

lnkDynamic.ID =" lnkDynamic_" +< identifier> ;;

lnkDynamic.Text =" SomeText";

lnkDynamic.Command + = new CommandEventHandler(lnkDynamic_Process);

lnkDynamic.CommandName =< whatever> ;;

lnkDynamic.Visible = false;

this.Controls.Add(lnkDynamic);

}


这将创建尽可能多的单个按钮,因为你的购物车中有元素。


3)创建事件处理程序


private void lnkDynamic_Command(object sender,CommandEventArgs e)

{

//您处理个人的代码元素到这里

//使用e.CommandName计算出点击了哪个按钮


}


4 )将单独的动态按钮放在您需要的任何地方,例如,
在GridView,Repeater等的单元格中,让它们可见。

显然你需要调整上面的内容以满足你的特殊需求,

但它应该足够了让你开始。

Dead easy.

When your page loads, I''m assuming that you already know how many orders are
in the cart - mayhe you''re storing the cart in a Session variable, or
fetching it from a database or whatever - doesn''t really matter... The
following assumes that you''ve fetched it into

1) In Page_Init, not Page_Load, fetch your cart

2) For each element in the cart (order, product, whatever), create a new
LinkButton() object:

LinkButton lnkDynamic;

foreach(<whateverin <cart>)
{
lnkDynamic = new LinkButton();
lnkDynamic.EnableViewState = true;
lnkDynamic.ID = "lnkDynamic_" + <identifier>;
lnkDynamic.Text = "SomeText";
lnkDynamic.Command += new CommandEventHandler(lnkDynamic_Process);
lnkDynamic.CommandName = <whatever>;
lnkDynamic.Visible = false;
this.Controls.Add(lnkDynamic);
}

That will have created as many individual buttons as there are elements in
your cart.

3) Create the event handler

private void lnkDynamic_Command(object sender, CommandEventArgs e)
{
// your code to process the individual elements goes here
// use e.CommandName to work out which button was clicked

}

4) Place the individual dynamic buttons on your form wherever they need to
be e.g. in the cells of a GridView, Repeater etc and make them visible.
Obviously you will need to adapt the above to suit your particular needs,
but it should be enough to get you started.


马克,我没有找到智能感知列表任何名为Command的属性,如你所示的

LinkBut​​ton控件你的帖子当然,

LinkBut​​ton控件确实有一个OnCommand属性!那么如何获得每个LinkBut​​ton的

CommandName?


实际上我正在做的是我得到的总数没有。用户的订单

已从数据库中放置(使用存储过程)&在SqlDataReader中返回

。这是在VB

类文件中执行的功能:


命名空间商店

公共类ShopCart

Private sqlConn As New SqlConnection(" .....")

公共函数GetOrderCount(ByVal UserID As Integer)As

SqlDataReader

Dim sqlCmd作为SqlCommand

Dim sqlReader作为SqlDataReader

sqlCmd =新的SqlCommand(" CountOrders",sqlConn)

sqlCmd.CommandType = CommandType.StoredProcedure


尝试

使用sqlCmd

.Parameters.Add(" @ UserID" ;,SqlDbType.Int).Value =

用户ID

结束


sqlConn.Open()

sqlReader = sqlCmd.ExecuteReader

if(sqlReader.HasRows)那么

返回sqlReader

否则

什么都不返回

结束如果

Catch ex As Exception

抛出ex

结束尝试

结束功能

结束班级

结束命名空间


这是调用上述功能的ASPX代码:


<%@ Import Namespace =" Shop" %>

''在这里导入其他必要的命名空间


Sub Page_Load(.....)

Dim boShopCart As ShopCart

Dim lnkCount As LinkBut​​ton

Dim iCount As Integer = 1

Dim sqlReader As SqlDataReader


boShopCart =新ShopCart

sqlReader = boShopCart.GetOrderCount(iUserID)


while(sqlReader.Read)

If(sqlReader) .GetValue(0)1)然后

iTotalOrder = sqlReader.GetValue(0)

否则

pnlLinks.Visible = False

结束如果

结束时


对于iCount = 1到iTotalOrder

lnkCount =新LinkBut​​ton

lnkCount.ID =" lnkCount" &安培; iCount

lnkCount.Text = iCount.ToString& "& nbsp;& nbsp;"

lnkCount.CommandName = iCount

pnlLinks.Controls.Add(lnkCount)

下一页

结束子


假设用户已经下了5个订单,上面的ASPX代码是

显示5个LinkBut​​tons 1 2 3 4 5 .....没问题,直到这个

点,但我如何得到每个LinkBut​​ton的CommandName。


我的主要目的是得到LinkBut​​tons的CommandName。事先

到此,我正在使用HTML超链接显示1 2 3 4 5&传递它

作为查询字符串。对于例如超链接的href值是,

比如说ShowOrder.aspx?OrderID = 23& PageNum = 1这样当用户点击

1时,1不再存在一个超链接。


Mark Rae写道:
Mark, I don''t find Intellisense list any property named Command for the
LinkButton control as you have shown in your post but of course, the
LinkButton control does have a OnCommand property! So how do I get the
CommandName of each LinkButton?

Actually what I am doing is I am getting the total no. of orders a user
has placed from the database (using a stored procedure) & returning it
in a SqlDataReader. This is the function which does that in the VB
class file:

Namespace Shop
Public Class ShopCart
Private sqlConn As New SqlConnection(".....")
Public Function GetOrderCount(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader

sqlCmd = New SqlCommand("CountOrders", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

Try
With sqlCmd
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
End With

sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
If (sqlReader.HasRows) Then
Return sqlReader
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace

This is the ASPX code that invokes the above function:

<%@ Import Namespace="Shop" %>
''importing other necessary Namespaces here

Sub Page_Load(.....)
Dim boShopCart As ShopCart
Dim lnkCount As LinkButton
Dim iCount As Integer = 1
Dim sqlReader As SqlDataReader

boShopCart = New ShopCart
sqlReader = boShopCart.GetOrderCount(iUserID)

While (sqlReader.Read)
If (sqlReader.GetValue(0) 1) Then
iTotalOrder = sqlReader.GetValue(0)
Else
pnlLinks.Visible = False
End If
End While

For iCount = 1 To iTotalOrder
lnkCount = New LinkButton
lnkCount.ID = "lnkCount" & iCount
lnkCount.Text = iCount.ToString & "&nbsp;&nbsp;"
lnkCount.CommandName = iCount
pnlLinks.Controls.Add(lnkCount)
Next
End Sub

Assuming that a user has placed 5 orders, the above ASPX code is
displaying the 5 LinkButtons 1 2 3 4 5.....no problem till this
point but how do I get the CommandName of each LinkButton.

My main intention is to get the CommandName of the LinkButtons. Prior
to this, I was using HTML hyperlinks to show 1 2 3 4 5 & passing it
as a querystring. For e.g. the href value of the hyperlink would be,
say, ShowOrder.aspx?OrderID=23&PageNum=1 so that when the user clicks
1, 1 no longer remains a hyperlink.

Mark Rae wrote:

< rn ** @ rediffmail.comwrote in message

news:11 ********************* @ k70g2000cwa.googlegro ups.com ......
<rn**@rediffmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

在购物车应用程序中,假设用户已经下了5个订单,我想

向他展示5个LinkBut​​tons(每个订单一个),这样当他点击
$时b $ b首先是LinkBut​​ton,他会看到他第一个订单的详细信息。

同样,如果他点击第二个LinkBut​​ton,他将会看到

第二个订单的详细信息命令他放了。 LinkBut​​tons的文本

将是1 2 3等。所以这个用户会看到1 2 3 4 5作为

LinkBut​​tons。


问题是如何动态添加LinkBut​​tons。我是否必须首先创建一个用户控件&
然后使用Page.LoadControl将

LinkBut​​tons添加到ASPX页面?或者还有其他出路吗?


有人可以建议我如何去做吗?
In a shopping cart app, suppose a user has placed 5 orders, I want to
show him 5 LinkButtons (one for each order) so that when he clicks the
first LinkButton, he would be shown the details of his first order.
Likewise if he clicks the second LinkButton, he will be shown the
details of the second order he had placed. The Text of the LinkButtons
will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the
LinkButtons.

The problem is how do I add the LinkButtons dynamically. Do I have to
first create a user control & then using Page.LoadControl add the
LinkButtons to the ASPX page? Or is there some other way out?

Can someone suggest me how do I go about it?



很容易死。


当你的页面加载时,我假设你已经知道有多少订单<购物车中的
- 可能你将购物车存放在一个Session变量中,或者是从一个数据库或其他任何东西中取出它来获取它 - 并不重要......

以下假设您已将其取入


1)在Page_Init中,而不是Page_Load,取出您的购物车


2)对于购物车中的每个元素(订单,产品等),创建一个新的

LinkBut​​ton()对象:

LinkBut​​ton lnkDynamic ;


foreach(< whateverin< cart>)

{

lnkDynamic = new LinkBut​​ton();

lnkDynamic.EnableViewState = true;

lnkDynamic.ID =" lnkDynamic_" +< identifier> ;;

lnkDynamic.Text =" SomeText";

lnkDynamic.Command + = new CommandEventHandler(lnkDynamic_Process);

lnkDynamic.CommandName =< whatever> ;;

lnkDynamic.Visible = false;

this.Controls.Add(lnkDynamic);

}


这将创建尽可能多的单个按钮,因为你的购物车中有元素。


3)创建事件处理程序


private void lnkDynamic_Command(object sender,CommandEventArgs e)

{

//您处理个人的代码元素到这里

//使用e.CommandName计算出点击了哪个按钮


}


4 )将单独的动态按钮放在您需要的任何地方,例如,
在GridView,Repeater等的单元格中,让它们可见。


显然你需要调整上面的内容以满足你的特殊需求,

但是它应该足以让你入门。


Dead easy.

When your page loads, I''m assuming that you already know how many orders are
in the cart - mayhe you''re storing the cart in a Session variable, or
fetching it from a database or whatever - doesn''t really matter... The
following assumes that you''ve fetched it into

1) In Page_Init, not Page_Load, fetch your cart

2) For each element in the cart (order, product, whatever), create a new
LinkButton() object:

LinkButton lnkDynamic;

foreach(<whateverin <cart>)
{
lnkDynamic = new LinkButton();
lnkDynamic.EnableViewState = true;
lnkDynamic.ID = "lnkDynamic_" + <identifier>;
lnkDynamic.Text = "SomeText";
lnkDynamic.Command += new CommandEventHandler(lnkDynamic_Process);
lnkDynamic.CommandName = <whatever>;
lnkDynamic.Visible = false;
this.Controls.Add(lnkDynamic);
}

That will have created as many individual buttons as there are elements in
your cart.

3) Create the event handler

private void lnkDynamic_Command(object sender, CommandEventArgs e)
{
// your code to process the individual elements goes here
// use e.CommandName to work out which button was clicked

}

4) Place the individual dynamic buttons on your form wherever they need to
be e.g. in the cells of a GridView, Repeater etc and make them visible.
Obviously you will need to adapt the above to suit your particular needs,
but it should be enough to get you started.


这篇关于动态LinkBut​​ton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆