VNIT

Too lazy to think of a slogan

Trang chủ » Diễn Đàn » Lập trình và Phát triển Web » Microsoft .NET » asp:repeater and button

Chủ đề đã bị khóa, bạn không thể xóa, sửa hay trả lời trong chủ đề này!

First Page Previous Page  1  Next Page Last Page
Locked asp:repeater and button
0
question Gởi bởi phuongt (1:50 03-07-2011)
Bài: 1,736 / Điểm VCS: 1,686 /

I want to have buttons inside repeater, so user can click on button and go to another page.

$(document).ready(function() {
            $("#container input:button").click(function() {
                var num = $('texthidden').val().split(",");
                if (($('texthidden').val().length != 0) && (num.length == 2)) window.location.href = "/ConfirmFlight.aspx?journey=" + $('texthidden').val();
            });
        });
<table border="1" >
    <asp:Repeater ID="search_result" runat="server" >
          <HeaderTemplate><tr><th>Dept.</th></tr></HeaderTemplate>
          <ItemTemplate>
        <tr><td><%#Eval (Container.DataItem,'op')%></td></tr>
                <tr><td><asp:button id="button" runat="server" Text="buuton" onClientClick="return true" OnClick="btn_click" /></td></tr>
            </ItemTemplate>
    </asp:Repeater>
</table>
I want user click on button and postback to server, I will redirect to another page
Because repeater control repeats many times, so it creates many button with the same id="button" and postback will be crashed. I want to set id ="button<%# Container.indexItem %>" but it does not compile due to invalid id syntax

if I use
<input type="button" value="button" /> and user click on button I use javascript to go another page, but it is always postback.

The question I want to ask for help is.
Is is possible if I want asp:button inside the repeater? any control with postback I can put inside the repeater ?

if I use HTML control without postback why input type="button" is not working?
Locked Re: asp:repeater and button
0
answer Gởi bởi phuongt (7:07 03-07-2011)
Bài: 1,736 / Điểm VCS: 1,686 /

The last attempt I did as

<table border="1" cellpadding="5" cellspacing="2" width="100%"><tr>
    <asp:Repeater ID="search_summay" runat="server">
        <ItemTemplate>
                <td align="center">
                            <table>......</table></td>
        </ItemTemplate>
    </asp:Repeater>
</tr></table>
///at this point I think OK
<table border="1" cellpadding="5" cellspacing="2" width="100%" >
    <asp:Repeater ID="search_result" runat="server" OnItemDataBound="OnTeamRepeaterItemDataBound" OnItemCommand="...." >
          <ItemTemplate>
              <tr><td>......</td class='parent'></tr> //showing data
              <tr class="detail" ><td colspan="11" > //hiding data
                    <table border="0" cellpadding="5" cellspacing="2" width="100%">
                          <asp:Repeater ID="outbound" runat="server" OnItemDataBound="OnOutLegItemDataBound">
                              <ItemTemplate>
    <asp:Repeater ID="outleg" runat="server">
          <ItemTemplate>
                                              <tr><td style="width:33%">.....</td></tr>
                                          </ItemTemplate>
    </asp:Repeater>
      </ItemTemplate>
        <SeparatorTemplate>
                                            <tr><td colspan="3"><hr style="width:60%" /></td></tr></SeparatorTemplate>
          </asp:Repeater>
                      </table> //table for outbound
      <hr />
                      <table border="0" cellpadding="5" cellspacing="2" width="100%">
                          <asp:Repeater ID="inbound" runat="server" OnItemDataBound="OnInLegItemDataBound">
                              <ItemTemplate>
    <asp:Repeater ID="outleg" runat="server">
          <ItemTemplate>
                                              <tr><td style="width:33%">.....</td></tr>
                                          </ItemTemplate>
    </asp:Repeater>
      </ItemTemplate>
        <SeparatorTemplate>
                                            <tr><td colspan="3"><hr style="width:60%" /></td></tr></SeparatorTemplate>
          </asp:Repeater>
                      </table> //table for inbound
      <hr />
                      <table border="0" cellpadding="5" cellspacing="2" width="100%">
                          <tr><td><asp:Button ID="button" runat="server" CommandName="SelectFlight"  OnClientClick="return CheckSelect(this)" Text="select" /></td></tr>
                      </table>  //table for command, click on button then clientvalidate as if any radio button has checked then postback
                    </ItemTemplate>
                </asp:Repeater>
            </table>
//client validate
<script type="text/javascript">
        function CheckSelect(ctrl) {
            var text = '';
            var tbs = $('#'+ctrl.id).closest("table").siblings("table");
            tbs.each(function () {
                $(this).find('input:radio').each(function (index) {
                    if ($(this).attr('checked')) {
                        if (text.length == 0) text = index;
                        else text += "," + index;
                    }
                });
            });
            var num = (text+"").split(",");
            if ((text.length != 0) && (num.length == tbs.length)) {
                $('#texthidden').val(text);
                return true;  //if radio button has been checked, return true to be ready postback
            } else {
                alert("Please select segment");
                return false;
            }
        }
        $(document).ready(function() {
            $("td.parent").click(function() {  //showing detail
                $(this).parent("tr").next("tr").slideToggle("fast");
                $(this).toggleClass("active");
            });
        });
    </script>
//server side code to run when button click
protected void OnTeamRepeaterItemCommand(object source, RepeaterCommandEventArgs e)    {
        Response.Redirect("ConfirmFlight.aspx?journey=" + e.CommandArgument+","+texthidden.Value);
    }

The problem I got so far is I use VS2010 and test as http://localhost:5408/mypage.aspx
when I click on button it does client validate OK, then waiting for host response.
then it crashes and saying 'set Enablevalidation=true on web.config or <% ...%>'

But I simulate with
<asp:Repeater ID="search_result" runat="server" OnItemDataBound="OnTeamRepeaterItemDataBound" OnItemCommand="...." >
          <ItemTemplate>
                <table>... //I set table here instead of asp:Repeater redering data
                <table>...
                <table>...
          </ItemTemplate>
  </asp:Repeater>

Then everything is OK
The code client validate and server are the same
First Page Previous Page  1  Next Page Last Page

Thống kê

Hiện tại web site có 85,806 thành viên. Xin chào đón thành viên mới nhất nightt.

Các thành viên đã tạo 62,822 chủ đề và 241,950 bài viết trong 30 box.

Hiện có 0 thành viên và 1 khách đang trực tuyến.

Template by styleshout / Icons by Tango Icon Library and FamFamFam.