vb.net 在ASP.Net/VB.Net中动态添加链接按钮)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18657544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:57:54  来源:igfitidea点击:

Dynamically add link buttons in ASP.Net/VB.Net)

asp.netvb.net

提问by suresh pareek

How can I make dynamically add link buttons to a form in asp.net(vb.net)?

如何在asp.net(vb.net)中动态添加链接按钮到表单?

Here is the code I tried:

这是我试过的代码:

<form id="form1" runat="server">
    <% For index As Integer = 1 To 10 %>
        <asp:LinkButton ID="#EVAL<%=index%>" runat="server"><%=index%></asp:LinkButton>
    <% Next %>
</form>

I tried dynamically using panel, but this create problem when use brtag then that will not give new line.

我尝试动态使用面板,但是在使用br标签时会产生问题,然后不会给出新行。

That code

那个代码

Dim lk As LinkButton

For index As Integer = 1 To 10
    lk = New LinkButton
    lk.ID = index
    lk.Text = index
    Panel1.Controls.Add(lk)
Next

采纳答案by Nicholas Post

Instead of trying to loop through them on the front end code, I would recommend using a panel and adding the controls to that.

我建议不要尝试在前端代码中循环遍历它们,而是建议使用面板并向其添加控件。

First you would create your panel:

首先,您将创建面板:

<form id="form1" runat="server">
    <ASP:Panel Runat="server" ID="Panel1" />
</form>

Then on the code behind, you would create your links dynamically and add them to that panel:

然后在后面的代码中,您将动态创建链接并将它们添加到该面板:

    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        Panel1.Controls.Add(lk)
    Next

If you need to wire events up to each link, you would use the AddHandler and attach the proper sub:

如果您需要将事件连接到每个链接,您可以使用 AddHandler 并附加适当的子项:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        AddHandler lk.Click, AddressOf DoSomething
        Panel1.Controls.Add(lk)
    Next
End Sub

Sub DoSomething(ByVal sender As Object, ByVal e As EventArgs)
    'Handle click here
End Sub

To resolve the issue with a line break, simple create a label with a <br> tag in it:

要解决换行符的问题,只需创建一个带有 <br> 标签的标签:

    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        AddHandler lk.Click, AddressOf DoSomething
        Panel1.Controls.Add(lk)

        Dim lbl as new label
        lbl.text = "<br>"
        Panel1.Controls.Add(lbl)
    Next

回答by Jeet Bhatt

Try this,

尝试这个,

<script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton lnk1 = new LinkButton();
            lnk1.Text = "testtest";

            LinkButton lnk2 = new LinkButton();
            lnk2.Text = "test";
            pnl1.Controls.Add(lnk1);
            pnl1.Controls.Add(lnk2);
        }
    </script>

<asp:Panel ID="pnl1" runat="server">
    </asp:Panel>

回答by brian

This is an example of how I did it using a mix of the previous answers. Also gets data form a mysql database to populate the panel with buttons.

这是我如何混合使用先前答案的示例。还从 mysql 数据库获取数据以使用按钮填充面板。

  • now set up the available audits for this client
  • they will be based on the id and the audits table
  • now get data from audits table
  • 现在为此客户设置可用的审计
  • 它们将基于 id 和审计表
  • 现在从审计表中获取数据

Code

代码

    Dim myStrSql As String = "SELECT audit,user_id,total_questions FROM audits WHERE user_id = " & myGlobalId & ";"
    Dim mySet As New DataSet
    Dim myda As New MySql.Data.MySqlClient.MySqlDataAdapter
    Dim myAudit As String = ""
    Dim myTotalQuestions As Integer = 0

    openMySql()

    myda = New MySql.Data.MySqlClient.MySqlDataAdapter(myStrSql, myConnection)
    myda.Fill(mySet, "SOURCE")

    Dim i As Integer = 0

    If mySet.Tables("SOURCE").Rows.Count > 0 Then
        For i = 0 To mySet.Tables("SOURCE").Rows.Count - 1
            myAudit = IIf(IsDBNull(mySet.Tables("SOURCE").Rows(i).Item("audit").ToString), "", mySet.Tables("SOURCE").Rows(i).Item("audit").ToString)
            myTotalQuestions = IIf(IsDBNull(mySet.Tables("SOURCE").Rows(i).Item("total_questions").ToString), 0, CInt(mySet.Tables("SOURCE").Rows(i).Item("total_questions").ToString))
            'now create a button for each
            Dim btn As New Button()
            btn.ID = "btn_" & Replace(Replace(Replace(Replace(Replace(Replace(myAudit, ",", ""), ".", ""), ":", ""), ";", ""), ":", ""), "?", "")
            btn.Text = myAudit & " : " & myTotalQuestions & " questions."
            AddHandler btn.Click, AddressOf MenuButtonClick
            Panel1.Controls.Add(btn)
            'this puts in a new line in the panel
            Dim lbl As New Label
            lbl.Text = "<br>"
            Panel1.Controls.Add(lbl)
        Next

    End If
End If

回答by Ann L.

Are you asking how to create a <BR/>tag dynamically?

您是在问如何<BR/>动态创建标签吗?

Try this:

尝试这个:

Dim lk As LinkButton 
Dim br as HtmlGenericControl

 For index As Integer = 1 To 10
     lk = New LinkButton
     lk.ID = index
     lk.Text = index
     Panel1.Controls.Add(lk)

     br = New HtmlGenericControl("br")
     Panel1.Controls.Add(br)
 Next

This is all from memory, but that's the general idea.

这都是凭记忆,但这是一般的想法。

ETA: BTW, I'm not sure indexwill work as the link button ID. I think IDs need to start with an alpha character.

ETA:顺便说一句,我不确定index是否可以用作链接按钮 ID。我认为 ID 需要以字母字符开头。