自动生成的代码里的参数应用
时间:2007-06-28 来源:chenqiubite
在DataGrid这个控件里编辑模版(ItemTemplate)里添加一图片按钮:
整个DataGrid控件的代码如下:
<asp:datagrid id="UserList" runat="server" Font-Names="Arial" Font-Size="12px" AllowPaging="True" Height="0px" Width="542px" BorderColor="#E6E7E8" BackColor="White" BorderWidth="1px" CellPadding="4" BorderStyle="None" AutoGenerateColumns="False">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#EBF8FE"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="12px" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#FF9900"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="column_name" HeaderText="栏目名"></asp:BoundColumn>
<asp:BoundColumn DataField="column_desc" HeaderText="栏目描述"></asp:BoundColumn>
<asp:BoundColumn DataField="column_time" HeaderText="建立时间" DataFormatString="{0:yyyy.M.d HH:mm}"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate><FONT face="宋体"></FONT></HeaderTemplate>
<ItemTemplate><FONT face="宋体"> </FONT>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Detail" CommandArgument='<%# ((System.Data.DataRowView)Container.DataItem)["column_id"] %>' ImageUrl="images/button_detail.gif" OnClick="ImageButton1_Click1" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Size="12px" Font-Names="Arial" HorizontalAlign="Left" ForeColor="Blue" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:datagrid> 图片按钮的代码摘下: <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Detail" CommandArgument='<%# ((System.Data.DataRowView)Container.DataItem)["column_id"] %>' ImageUrl="images/button_detail.gif" OnClick="ImageButton1_Click1" /> 双击此图片按钮(生成代码): protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
ImageButton ibt = (ImageButton)sender;
switch(ibt.CommandName)
{
case "Detail":
this.Response.Redirect("ArticleList.aspx?id=" + ibt.CommandArgument);
break;
}
} 注意这里的红色的代码,sender对象的使用,CommandName和CommandArgument的使用(参数是如何传值的)。
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#EBF8FE"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="12px" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#FF9900"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="column_name" HeaderText="栏目名"></asp:BoundColumn>
<asp:BoundColumn DataField="column_desc" HeaderText="栏目描述"></asp:BoundColumn>
<asp:BoundColumn DataField="column_time" HeaderText="建立时间" DataFormatString="{0:yyyy.M.d HH:mm}"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate><FONT face="宋体"></FONT></HeaderTemplate>
<ItemTemplate><FONT face="宋体"> </FONT>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Detail" CommandArgument='<%# ((System.Data.DataRowView)Container.DataItem)["column_id"] %>' ImageUrl="images/button_detail.gif" OnClick="ImageButton1_Click1" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Size="12px" Font-Names="Arial" HorizontalAlign="Left" ForeColor="Blue" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:datagrid> 图片按钮的代码摘下: <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Detail" CommandArgument='<%# ((System.Data.DataRowView)Container.DataItem)["column_id"] %>' ImageUrl="images/button_detail.gif" OnClick="ImageButton1_Click1" /> 双击此图片按钮(生成代码): protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
ImageButton ibt = (ImageButton)sender;
switch(ibt.CommandName)
{
case "Detail":
this.Response.Redirect("ArticleList.aspx?id=" + ibt.CommandArgument);
break;
}
} 注意这里的红色的代码,sender对象的使用,CommandName和CommandArgument的使用(参数是如何传值的)。
相关阅读 更多 +