我有分页功能一个gridview。
下面的代码是GridView的我的aspx代码。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SAP No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"
Text='<%#Eval("SAPNO") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PARTNO" HeaderText="Part No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
</asp:BoundField>
<asp:BoundField DataField="PARTDESC" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="MINQTY" HeaderText="Min Qty" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="QOH" HeaderText="QOH" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="CATEGORY" HeaderText="Category" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="EQUIPMENT" HeaderText="Equipment" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.JPG" ControlStyle-Height="20px" ControlStyle-Width="50px" HeaderText="Delete">
<HeaderStyle Width="10%" HorizontalAlign="Left" /></asp:CommandField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="<--" PreviousPageText="<" NextPageText=">" LastPageText="-->" />
<PagerStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
在后端代码,我试图找回第一列和特定的行,但我扔空。下面的代码是我如何检索。
int index = Convert.ToInt32(e.RowIndex);
string sapNo1 = GridView1.Rows[index].Cells[0].Text;
string partNo = GridView1.Rows[index].Cells[1].Text;
string partDesc = GridView1.Rows[index].Cells[3].Text;
我尝试显示sapNo1所有的价值,它扔我的空白,但对于PARTNO它会显示在GridView中的数据。
任何人有这个什么想法?我真的很感谢您的帮助和评论!
作为您的第一列是一个LinkButton使检索,因为它是那么找到该值。
string sapNo1 = GridView1.Rows[index].Cells[0].Text; // wrong code to retrieve data from linkbutton in a gridview
这个修改为:
LinkButton linkbtn =(LinkButton)GridView1.Rows[index].FindControl("LinkButton1");
string sapNo1 = linkbtn.Text;
要么
string sapNo1 =(GridView1.Rows[index].FindControl("LinkButton1") as LinkButton).Text;
你不能得到控件的属性里面存在像TemplateField
,你需要找到这样的控制: -
LinkButton LinkButton1 = (LinkButton)GridView1.Rows[index].Cells[0]
.FindControl("LinkButton1");
这以后,就可以获取该控件的属性: -
string sapNo1 = LinkButton1.Text;
您可以通过下面的代码获取链接按钮说明
protected void Link_Button_Command(object sender, CommandEventArgs e)
{
try
{
LinkButton button = (LinkButton)sender;
string name = button.ToString();
}
catch (Exception ex)
{
lbl_message.Text = ex.Message;
}
}