【问题标题】:Pass value from a Repeater row to a textbox on button click in asp.net C#在asp.net C#中单击按钮时将值从Repeater行传递到文本框
【发布时间】:2023-03-14 05:25:02
【问题描述】:

我在更新面板中封装了以下中继器

     <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
             <Triggers>
                 <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
             </Triggers>
             <ContentTemplate>
                <asp:Repeater ID="skillTable" runat="server">
                    <ItemTemplate>
                        <table class="table table-hover">
                            <tr>
                                <td>
                                    <asp:ImageButton runat="server" AutoPostBack="True" ID="skillButton" OnClick="skillButton_Click" CommandArgument="<%# Eval(DropDownList1.SelectedValue)%>" class="addText btn btn-success" ImageUrl="~/img/addbut.png" /></td>
                                <td><asp:Label runat="server" id="skillName" Text='<%# DataBinder.Eval(Container.DataItem, DropDownList1.SelectedValue) %>'></asp:Label></td>
                            </tr>
                        </table>                
                    </ItemTemplate>
                </asp:Repeater>
            </ContentTemplate>
        </asp:UpdatePanel>

它完美地从我的数据库中剔除信息,并且在每一行的文本之前都有一个按钮。

我遇到的问题是,我需要每行上的每个按钮,当单击时,将该行中的特定代码行添加到文本框。

            foreach (RepeaterItem item in skillTable.Items)
    {
        string skill = item.DataItem.ToString();
        string text = skillList.Text;
        if (!string.IsNullOrEmpty(text))
        {

            if (!text.Contains(skill))
            {
                text += "  |  " + skill;
                skillList.Text = text;
            }
        }
        else
        {
            text = skill;
            skillList.Text = text;
        }
    }
    UpdatePanel2.Update();

我也试过这种方式,

     protected void skillTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    int d = 0;
        if(DropDownList1.SelectedValue == "Business and Finance")
        {
            d = 1;
        }
        else if(DropDownList1.SelectedValue == "Computers and Technology")
        {
            d = 2;
        }
        else if (DropDownList1.SelectedValue == "Education")
        {
            d = 3;
        }
        else if (DropDownList1.SelectedValue == "Customer Service")
        {
            d = 4;
        }
        DataRowView drv = (DataRowView)e.Item.DataItem;
        string skill = drv[d].ToString();
        Session["Table"] = skill;

}
protected void skillButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    string skill = (string)(Session["Table"]);
    string text = skillList.Text;
    if (!string.IsNullOrEmpty(text))
    {

        if (!text.Contains(skill))
        {
            text += "  |  " + skill;
            skillList.Text = text;
        }
    }
    else
    {
        text = skill;
        skillList.Text = text;
    }
    UpdatePanel2.Update();
}

但它们中的任何一个似乎都无法正常工作。有什么建议吗?在此之前我还没有真正使用过中继器,所以如果有任何其他工具可以更好地工作,我愿意提供建议。

提前致谢!

【问题讨论】:

  • 也许这个链接可能会有所帮助...stackoverflow.com/questions/16606208/…
  • @MethodMan 谢谢,我试过那个链接,但是,它返回转发器表中的所有内容。我只想在您单击特定行的按钮时拉出一个特定的行。不过感谢您的帮助!

标签: c# asp.net asprepeater


【解决方案1】:

所以我想通了。我缺少的是一种让我的网站专门缩小我需要的范围的方法。我在我的 click 方法中添加了代码打击并摆脱了 ItemDataBound 方法,它就像一个魅力。

    Button button = (sender as Button);
    string commandArgument = button.CommandArgument;
    RepeaterItem item = button.NamingContainer as RepeaterItem;
    var workText = (Label)item.FindControl("workName1") as Label;
    string work = workText.Text;
    string text = workDetails1.Text;
    if (text == "   ")
        text = "";

    if (text == "")
    {
        if (!text.Contains(work))
        {
            text +="\u2022 " + work;
            workDetails1.Text = text;
        }
    }
    else if (!string.IsNullOrEmpty(work))
    {
        if (!text.Contains(work))
        {
            text += "\n\u2022 " + work;
            workDetails1.Text = text;
        }
        else
        {
            workDetails1.Text = text;
        }
    }
    UpdatePanel4.Update();

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多