按钮Onclick在转发器内

时间:2014-12-05 14:51:06

标签: c# asp.net visual-studio onclick repeater

我创建了一个转发器,它从webbservice(Name,Surname和Number)中获取3个不同的值。根据我收到的数字是1还是0,有2个按钮设置为Visible = true或false。 (btnNumber0和btnNumber1)。

<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rptHB_ItemDataBound" OnItemCommand="rptHB_ItemCommand">     
    <ItemTemplate>
        <div>
            <h1 class="click">
                <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() %>'></asp:Label>
                <asp:Label ID="lblSurname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Surname").ToString() %>'></asp:Label>
                <asp:Label ID="lblNumber" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Numer").ToString() %>'></asp:Label>
            </h1>

            <div class="expand"> 
                <select id="optColor">
                    <option>Blue</option>
                    <option>Red</option>
                    <option>Yellow</option>                       
                    <option>Green</option>
                    <option>Black</option>
                </select>                  

                <asp:Button ID="btnNumber0" runat="server" Text="0"/>
                <asp:Button ID="btnNumber1" runat="server" Text="1"/>                            
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

我无法工作的是将onClick功能设为我的2按钮。在OnCick内部我想从我选择的人那里获取名称和颜色(optColor)并将其发送回我的webbservice。我是否必须使用OnItemCommand,如果可以的话,你可以帮助我吗?或者它是你能做到的另一种方式吗?

protected void Page_Load(object sender, EventArgs e)
{
    GetInfo();
}

private void GetInfo()
{
    rpt1.DataSource = client.SendInfo();
    rpt1.DataBind();
}

private bool Getbutton(string ValueNumber)
{
    return ValueNumber == "0";
}

protected void rptHB_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Label lblNewNumber = (Label)e.Item.FindControl("lblNumber");
    Label lblNewName = (Label)e.Item.FindControl("lblName");
    Button btnNewNumber0 = (Button)e.Item.FindControl("btnNumer0");
    Button btnNewNumber1 = (Button)e.Item.FindControl("btnNumber1");
    string Number = lblNewNumber.Text;
    btnNewNumber0.Visible = GetButton(Number);
    btnNewNumber1.Visible = !GetButton(Number);
 }

protected void rptHB_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    /* ??? */
}

1 个答案:

答案 0 :(得分:4)

如果我理解你正确,你想要处理这两个按钮的点击事件,并获得该RepeaterItem人物的名称和颜色的值。您可以使用相同的处理程序:

ASPX:

<asp:Button ID="btnNumber0" OnClick="btn_Click" runat="server" Text="0"/>
<asp:Button ID="btnNumber1" OnClick="btn_Click" runat="server" Text="1"/>   

代码隐藏:

protected void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button) sender;
    RepeaterItem item = (RepeaterItem) btn.NamingContainer;
    Button btnNewNumber0 = (Button)item.FindControl("btnNumer0");
    Button btnNewNumber1 = (Button)item.FindControl("btnNumer1");
    bool btn0Clicked = btn == btnNewNumber0;
    btnNewNumber0.Visible = !btn0Clicked;
    btnNewNumber1.Visible = btn0Clicked;
    // now call your webservice, you have all you need here
    Label lblName = (Label) item.FindControl("lblName"); 
    Label lblSurname = (Label) item.FindControl("lblSurname"); 
    Label lblNumber = (Label) item.FindControl("lblNumber"); 
    DropDownList ddlColor = (DropDownList) item.FindControl("ddlColor");
    // now call your webservice, you get the color-selection via ddlColor.SelectedValue
}

如果您想在服务器端访问它,还可以使用DropDownList代替HTML Select