首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

datalist 点击事件取值,再传值有关问题,高手们帮忙看看代码

2011-12-14 
datalist 点击事件取值,再传值问题,高手们帮忙看看代码怎样取出当前点击图片地址?目前代码,已经可以取到值

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>
  <br />
  </ItemTemplate>


  </asp:DataList>





[解决办法]
好好调试一下。绑定放到page_load里并且加上if(!ispostback){} 仔细点就能找到问题了。
[解决办法]

探讨
引用:
如果按照你那个写 BUTTON 事中的代码 该写在哪呀 ?

热点排行