我正在创建一个应用程序,我在其中显示数据列表中的图像(包含在文件夹中)。每个datalist单元格都有一个ImageButton(单击将显示图像的大图片),一个删除按钮(单击将删除图像),一个编辑按钮和一个文本框。单击编辑按钮将使图像文件名称显示在文本框中。
这是我的.aspx代码
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5" EnableViewState="True">
<ItemTemplate>
<asp:ImageButton Width="100" ID="ImagePic" ImageUrl='<%# Container.DataItem %>' CommandName='<%# Container.DataItem %>' runat="server" OnClick="ImagePic_Click" ImageAlign="Top">
</asp:ImageButton>
<br />
<asp:Button Width="100" ID="btn_image_del" CommandName='<%# Container.DataItem %>' runat="server" Text="Delete" OnClick="btn_image_del_Click">
</asp:Button>
<br />
<asp:TextBox ID="txt_image_name" Width="100" runat="server" Visible="True" Text='<%# Container.DataItem %>' MaxLength="500" />
</asp:TextBox>
<asp:Button Width="100" ID="btn_image_edit" CommandName='<%# Container.DataItem %>' runat="server" Text="Edit" OnClick="btn_image_edit_Click">
</asp:Button>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />
</asp:DataList>
.cs代码如下
protected void BindDataList()//shows the pics from the user folder
{
string[] list = Directory.GetFiles(Server.MapPath("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/"));
var aList = from fileName in Directory.GetFiles(Server.MapPath("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/"))
select string.Format("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/{0}", Path.GetFileName(fileName));
dtlist.DataSource = aList;
dtlist.DataBind();
}
protected void ImagePic_Click(object sender, ImageClickEventArgs e)
{
string strImage = ((ImageButton)sender).CommandName;
ViewState["InsertedURL"] = strImage;
ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:ShowPic();", true);
Large_Pic.ImageUrl = strImage;
}
protected void btn_image_del_Click(object sender, EventArgs e)
{
string del_selected_image = ((Button)sender).CommandName;
File.Delete(Server.MapPath(del_selected_image));
}
protected void btn_image_edit_Click(object sender, EventArgs e)
{
string edit_selected_image = Path.GetFileName(((Button)sender).CommandName);
// Now what should i do:
}
以上三个函数即ImagePic_Click,btn_image_del_Click,btn_image_edit_Click正常工作。删除,然后LargePic视图都完美运行,我的问题是,我希望当单击编辑按钮时,相应的图像名称将显示在Datalist文本框中。在上面的函数中,edit_selected_image保存了相应图像的文件名。我通过应用断点测试了它。现在的问题是我希望这个值应该传递给Datalist中的文本框“txt_image_name”。
答案 0 :(得分:0)
你需要使用EditItemTemplate来编辑Edit Template. 你可以像这样写下你的标记。
<EditItemTemplate>
PictureName:<asp:Label id="PicutreLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "yourFIleNamePath") %>' runat="server"/> <br />
<asp:LinkButton id="UpdateButton" Text="Update" CommandName="Update" runat="server"/>
<asp:LinkButton id="DeleteButton" Text="Delete" CommandName="Delete" runat="server"/>
<asp:LinkButton id="CancelButton" Text="Cancel" CommandName="Cancel" runat="server"/>
答案 1 :(得分:0)
试试这个:
TextBox txt =(TextBox)dtlist.FindControl("txt_image_name");
txt.Text = edit_selected_image;