日期:2014-05-18  浏览次数:20801 次

datalist 点击事件取值,再传值问题,高手们帮忙看看代码
怎样取出当前点击图片地址?
目前代码,已经可以取到值, 但是是固定的, string picurl = ((ImageButton)(DataList1.Items[0].FindControl("ImageButton1"))).ImageUrl.ToString();
正确的应该怎样写, 
或者有更好的方法?

类里的代码

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  {
  if (Session["username"] == null)
  {
  Response.Write("<script>alert('请登录后查看');history.go(-1);</script>");
  Response.End();
  }
  else
  {
  string picurl = ((ImageButton)(DataList1.Items[0].FindControl("ImageButton1"))).ImageUrl.ToString();
  SqlConnection con1 = new SqlConnection("server=*");
  con1.Open();
  SqlDataAdapter sd = new SqlDataAdapter("select * from upimage where picUrl='" + picurl + "'", con1);
  DataSet ds1 = new DataSet();
  sd.Fill(ds1);
  string id = ds1.Tables[0].Rows[0]["id"].ToString();

  Response.Redirect("uppic_viewxx.aspx?id=" + id);

  }
  }

前台控件代码

<asp:DataList ID="DataList1" runat="server" RepeatColumns="4"
  RepeatDirection="Horizontal" UseAccessibleHeader="True" Width="100%">
  <ItemTemplate>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:ImageButton ID="ImageButton1"
  runat="server" Height="110px" ImageUrl='<%# Eval("picUrl") %>' OnClick="ImageButton1_Click" Width="127px" />
  <a href='<%# Eval("ID", "uppic_viewxx.aspx?id={0}") %>'>
  </a>&nbsp;<br />
  <asp:Label ID="lblTime" runat="server" Text='<%# Eval("picTime","{0:d}") %>' Width="89px"></asp:Label><br />
  <table align="center" border="0" cellpadding="0" cellspacing="0" width="50">
  <tr>
  <td width="3">
  <img height="20" src="images/picname_55.jpg" width="3" /></td>
  <td align="center" background="images/picname_57.jpg" style="width: 74px">
  <a href='<%# Eval("ID", "uppic_viewxx.aspx?id={0}") %>'>
  <asp:Label ID="lblPicname" runat="server" CssClass="zcb12" Text='<%# Eval("picName") %>'
  Width="87px"></asp:Label></a></td>
  <td style="width: 27px">
  <img height="20" src="images/picname_58.jpg" width="3" /></td>
  </tr>
  </table>