从Repeater获取数据

时间:2011-08-03 05:47:49

标签: c# repeater

基本上问题是:

如何从转发器获取ImageID,以便我可以在按钮单击时使用查询字符串传递它?

例如:我需要编辑图像细节,每个图像都有一个编辑按钮

如下图所示:(由转发器生成)

enter image description here

c#c​​ode:

var List = new List<Images>();
                foreach (DataRow dr in picture.Rows)
                {
                    DateTime DateTemp =            DateTime.Parse(dr["date"].ToString());
                    var childPhoto = new Images()
                               {
                                    DateTaken = DateTemp.ToString("MM/dd/yyyy"),
                                    PlaceTaken = dr["place"].ToString(),
                                    DetailedInfo = dr["info"].ToString(),
                                    ImageID = dr["ImageId"].ToString()
                               };
                    childList.Add(childPhoto);
                }

                ShowPhotoRepeater.DataSource = List;
                ShowPhotoRepeater.DataBind();
            }
        }
    }

    protected void btnEditChildPhoto_Click(object sender, EventArgs e)
    {

        Response.Redirect("EditPhoto.aspx?ImageID=" + ));
    }

我的aspx代码:

 <asp:Repeater ID="ShowProfileRepeater" runat="server">
    <ItemTemplate>
    <table> 
        <tr>
            <td width = "15%" rowspan="6"><%--<asp:Image ID="ImgPhoto" runat=server />--%> 
            <%--<asp:Image ID="childImage" ImageUrl="<%#Eval("ImgUrl")%>" runat="server" />--%>  
            <asp:Image ID="childImage" ImageUrl="~/img/missing children pictures/shearwey.jpg" CssClass="content_image" runat="server" /> 
            </td><br />
            <td width = "15%">Name:</td>
            <td width = "70%">
                <asp:Label ID="lblChildName" runat="server" Text='<%#Eval("Name")%>'></asp:Label></td>
            <td >
                <asp:Button ID="btnEditChildProfile" runat="server" Text="Edit"   />
            </td>

        </tr>
        <tr>
            <td width = "15%">Gender:</td>
            <td width = "55%">
                <asp:Label ID="lblChildGender" runat="server" Text='<%#Eval("Gender")%>'></asp:Label></td>
            <td></td>
        </tr>
        <tr>
            <td width = "10%">Date Of Birth:</td>
            <td width = "55%">
                <asp:Label ID="lblChildDOB" runat="server" Text='<%#Eval("DOB")%>'></asp:Label></td>                                  
            <td></td>
        </tr>
        <tr>
            <td width = "10%">Country:</td>
            <td width = "55%">
                <asp:Label ID="lblChildCountry" runat="server" Text='<%#Eval("Country")%>'></asp:Label></td>                                  
            <td></td>
        </tr>
        <tr>
            <td width = "10%">Missing Date:</td>
            <td width = "55%">
                <asp:Label ID="lblChildMissingDt" runat="server" Text='<%#Eval("MissingDt")%>'></asp:Label></td>                                  
            <td></td>
        </tr>
        <tr>
            <td width = "10%">Last Seen Location:</td>
            <td width = "55%">
                <asp:Label ID="lblChildLaseSeenLoc" runat="server" Text='<%#Eval("LastSeenLoc")%>'></asp:Label></td>                                  
            <td></td>
        </tr>

    </table>
    </ItemTemplate>
    </asp:Repeater> 

2 个答案:

答案 0 :(得分:1)

<asp:Button ID="btnEditChildProfile" runat="server" Text="Edit" CommandArgument='<%# Eval("ImageId")%>'   />



protected void btnEditChildPhoto_Click(object sender, EventArgs e)
{
    var btn = (Button)sender;
    Response.Redirect("EditPhoto.aspx?ImageID=" + btn.CommandArgument));
}

<a href='EditPhoto.aspx?ImageID=<%# Eval("ImageId") %>' >Edit</a>

答案 1 :(得分:1)

不使用按钮,而是使用转发器内的链接按钮重定向到所需的链接。这样,您不需要为按钮单独的事件处理程序。

 <asp:LinkButton PostBackUrl="EditPhoto.aspx?ImageID=<%#Eval("ImageId")%>"></asp:LinkButton>