GridView,Asp.net,GridView中列数据的几种使用方法
GridView中列数据的几种使用方法
第一种:TemplateField,直接赋值
<asp:TemplateField HeaderText="工程编号">
<HeaderStyle Width="70px" />
<ItemStyle Width="70px" />
<ItemTemplate>
<%#Eval("job_no")%>
</ItemTemplate>
</asp:TemplateField>
但这种方法在后续使用中不便引用,一般不推荐。
第二种:TemplateField+label,使用时需要FindControl
<asp:TemplateField HeaderText="合同号">
<HeaderStyle Width="70px" />
<ItemStyle Width="70px" />
<ItemTemplate>
<asp:Label runat="server" ID="contract_no" Text='<%#Eval("contract_no")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
数据使用方法:需要FindControl
GridViewRow gvr = gvList.Rows[e.RowIndex];
Label strContract_no = (Label)gvr.FindControl("contract_no");
完整示例:
GridViewRow gvr = gvList.Rows[e.RowIndex];
Button btnUpload = (Button)gvr.FindControl("btnUploadRow");
Label strContract_no = (Label)gvr.FindControl("contract_no");
IFileService fs = FileService.Instance;
string strPath = @"\" + fs.GetServerIP() + @"e$WebSystemAttachContract_Invoice_Attach" + "\" + strContract_no.Text;
fs.ShowManage(btnUpload, "SXF", "1", "1", strPath, "", "", "发票上传【" + strContract_no.Text + "】");
第三种,BoundField,直接调用
<asp:BoundField DataField="contract_no" HeaderText="合同号" >
<HeaderStyle Width="70px" HorizontalAlign="Center" />
<ItemStyle Width="70px" HorizontalAlign="Center" />
</asp:BoundField>
数据使用方法简单:可以直接引用
string strContract_no = gvr.Cells[2].Text;t
完整示例:
GridViewRow gvr = gvList.Rows[e.RowIndex];
Button btnUpload = (Button)gvr.FindControl("btnUploadRow");
string strContract_no = gvr.Cells[2].Text;
IFileService fs = FileService.Instance;
string strPath = @"\" + fs.GetServerIP() + @"e$WebSystemAttachContract_Invoice_Attach" + "\" + strContract_no;
fs.ShowManage(btnUpload, "SXF", "1", "1", strPath, "", "", "发票上传【" + strContract_no + "】");
此外,长字段显示处理方法:避免折行,截取部分显示,并加tooltip,鼠标显示提示
<asp:TemplateField HeaderText="工程名称">
<HeaderStyle Width="150px" />
<ItemStyle Width="150px" />
<ItemTemplate>
<asp:Label runat="server" ID="contract_no" Text='<%#Convert.ToString( Eval("job_name")).Length > 10 ? Convert.ToString( Eval("job_name")).Substring(0, 10)+"..." : Eval("job_name")%>' ToolTip='<%#Eval("job_name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
详细
Convert.ToString( Eval("job_name")).Length > 10 ? //如果job_name长度>10
Convert.ToString( Eval("job_name")).Substring(0, 10)+"..." : //截取前10位并加...
Eval("job_name")%>' // 否则显示全部
ToolTip='<%#Eval("job_name") //提示显示全部
列中使用按钮字段
<asp:TemplateField HeaderText="上传">
<ItemTemplate>
<asp:Button ID="btnUploadRow" runat="server" Text="发货单上传" Width="80px" CommandName="Update"
OnClientClick="return confirm('确定进行上传吗?');"/>
</ItemTemplate>
</asp:TemplateField>
事件代码:
protected void gvList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 使用BoundField,直接调用
GridViewRow gvr = gvList.Rows[e.RowIndex];
Button btnUpload = (Button)gvr.FindControl("btnUploadRow");
string strContract_no = gvr.Cells[2].Text;
IFileService fs = FileService.Instance;
string strPath = @"\" + fs.GetServerIP() + @"e$WebSystemAttachContract_Invoice_Attach" + "\" + strContract_no;
fs.ShowManage(btnUpload, "SXF", "1", "1", strPath, "", "", "发票上传【" + strContract_no + "】");
// 使用TemplateField+label
//GridViewRow gvr = gvList.Rows[e.RowIndex];
//Button btnUpload = (Button)gvr.FindControl("btnUploadRow");
//Label strContract_no = (Label)gvr.FindControl("contract_no");
//IFileService fs = FileService.Instance;
//string strPath = @"\" + fs.GetServerIP() + @"e$WebSystemAttachContract_Invoice_Attach" + "\" + strContract_no.Text;
//fs.ShowManage(btnUpload, "SXF", "1", "1", strPath, "", "", "发票上传【" + strContract_no.Text + "】");
}
end
【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=ysykkNnklIW2 Tag: GridView Asp.net