从gridview获取数据不会返回任何内容

时间:2012-10-22 18:23:55

标签: asp.net

我想通过下面的代码从我的网格视图的第二列获取数据,但它什么都不返回(di.Cells [1] .Text),请帮助解决问题:

protected void btnShowAddTransmittaltoCon_Click(object sender, EventArgs e)
{
    Transmittallistfortest transmittalList = (Transmittallistfortest)DetailsView1.FindControl("Transmittallistfortest1");
    GridView g3 = transmittalList.FindControl("GridViewTtransmittals") as GridView;
    foreach (GridViewRow di in g3.Rows)

    {

        RadioButton rad = (RadioButton)di.FindControl("RadioButton1");
        //Giving Error:Object reference not set to an instance of an object.
        if (rad != null && rad.Checked)
        {
            var w = di.RowIndex;

            Label1.Text = di.Cells[1].Text;
        }

aspx如下所示:

 <asp:GridView ID="GridViewTtransmittals" runat="server" AutoGenerateColumns="False" 
          BorderStyle="None" ShowHeader="False" Width="100%" DataKeyNames="TransID"  onrowdatabound="GridViewTtransmittals_RowDataBound"
        onrowdeleting="GridViewTtransmittals_RowDeleting">
          <columns>
              <asp:TemplateField>
                  <ItemTemplate>
                      <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Transmittal" AutoPostBack="True" 
        OnCheckedChanged="RadioButton1_CheckedChanged" />

                  </ItemTemplate>
              </asp:TemplateField>
            <asp:hyperlinkfield DataNavigateUrlFields="TransId" 
              DataNavigateUrlFormatString="~/Copy of Transmittal.aspx?TransId={0}" DataTextField="TRANSMITTAL" 
              HeaderText="Transmittal" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码

var result = ((HyperLink)di.Cells[1].Controls[0]).Text;