我正在創建一個應用程序,在該應用程序中,我在數據列表中顯示圖像(包含在文件夾中)。每個datalist單元格都有一個ImageButton(單擊它將顯示大圖片的圖片),一個刪除按鈕(單擊它將刪除圖像),一個編輯按鈕和一個文本框。單擊編輯按鈕將導致圖像文件名稱顯示在文本框中。向Datalist ItemTemplate中的TextBox添加值
這裏是我的.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視圖都完美運行,我的問題是,我想要點擊編輯按鈕時,相應的圖像名稱將顯示在Dat alist文本框。在上面的函數中,edit_selected_image保存了相應圖像的文件名。我通過應用斷點來測試它。現在問題是我希望這個值應該傳遞給Datalist中的文本框「txt_image_name」。
請閱讀我的總職位....當U回答我,我是編輯我的post..Please幫我哥們 – Saswat
我們還可以使用的ItemTemplate ...無論如何,我已經自己解決了 – Saswat