今天由于项目的需要,需要在Gridview中自定义一个模板列,在该列中分别放置了两个ImageButton的控件,一个是提供“删除”功能,一个是“编辑”功能,也许有人会说这不是Gridview中已经有的模板吗?但是我要实现的方式是那两者不能提供的!也许是我没有深入研究~~~ 现在提供下我自己的解决方案
1.在界面上:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderWidth="1px" BorderColor=black Font-Size="Small" AutoGenerateColumns="False" AllowPaging="True" BorderStyle="None" Width="576px" >
<FooterStyle BackColor="White" ForeColor="Black" HorizontalAlign=Right />
<RowStyle ForeColor="Black" BorderStyle="None" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" BorderStyle="None" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Right" BorderStyle="None" />
<HeaderStyle BackColor="#6A8CA5" Font-Bold="False" ForeColor="Black" Font-Size="Small" BorderColor=Black BorderStyle="None" />
<Columns>
<asp:BoundField DataField="Flow_No" HeaderText="关数" SortExpression="Flow_No" > </asp:BoundField>
<asp:TemplateField HeaderText="关数" >
<ItemTemplate >
<asp:Label ID="Label9" runat="server" Text="" ></asp:Label>
</ItemTemplate>
<asp:TemplateField >
<ItemTemplate >
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/dot/x.gif" ToolTip="Delete" Visible=false />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/dot/table.gif" ToolTip="Edit" Visible=false OnCommand="ImageButton1_click" CommandArgument='<%# Eval("Flow_ID") %>'/>
</ItemTemplate>
<ItemStyle BorderColor="Black" VerticalAlign="Middle" HorizontalAlign="Center" />
<HeaderStyle BorderColor="Black" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
</asp:GridView>
2.在code中
a.实例化Imagebutton1等控件
If (e.Row.RowType = DataControlRowType.DataRow Or e.Row.RowType = DataControlRowType.Header) Then
dim Flow_No as string
Flow_No=e.row.cells(0).text
Dim mylab As Label = e.Row.Cells(1).FindControl("Label9")
Dim myimage2 As ImageButton = CType(e.Row.Cells(9).FindControl("ImageButton1"), ImageButton)
Dim myimage3 As ImageButton = CType(e.Row.Cells(9).FindControl("ImageButton2"), ImageButton)
If Flow_No = "99" Then
mylab.Text = "--"
End If
If Flow_No = "0" Or Flow_No = "1" Or Flow_No = "2" Or Flow_No = "3" Then
mylab.Text = Flow_No
End If
If Flow_No = "1" Or Flow_No = "2" Or Flow_No = "3" Then
myimage3.Visible = True
End If
If Flow_No = "1" Or Flow_No = "2" Or Flow_No = "3" Or Flow_No = "99" Then
myimage2.Visible = True
End If
e.Row.Cells(0).Style.Add("display", "none")
End If
b.自定义该控件的事件
Public Sub ImageButton1_click(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim connstr1 = System.Configuration.ConfigurationManager.AppSettings("connstring")
Dim cn1 As SqlConnection = New SqlConnection(connstr1)
cn1.Open()
Dim Edit_id As String
Dim str_Edit As String
Edit_id = e.CommandArgument.ToString
str_Edit = "select * from " & Application("BPP_Owner") & ".yam_bpp_tsflowset where Flow_id=" & Edit_id
Dim comm_edit As SqlCommand = New SqlCommand(str_Edit, cn1)
Dim dr_comm As SqlDataReader = comm_edit.ExecuteReader
If dr_comm.Read Then
TextBox1.Text = dr_comm("flow_no")
TextBox2.Text = dr_comm("flow_no")
TextBox3.Text = dr_comm("Flow_Name")
If TextBox1.Text = "99" Then
TextBox4.Text = ""
DropDownList1.Enabled = False
Else
TextBox4.Text = dr_comm("preFlow_NO")
End If
End If
'TextBox1.Text = 1
'TextBox3.Text = "测试"
dr_comm.Close()
cn1.Close()
End Sub
其中2-a的是关于隐藏某一列但是其他列还是可以引用其值的方式。由于在该项目中有用到我就记录下来了!在这里关键是在1中ImageButton中的oncommand的使用跟CommandArgument的使用。第一次写这样的文章也许看起来很乱!