row.Cells [4] .Text在GridView1_SelectedIndexChanged中什么都不返回?

时间:2010-08-31 19:15:44

标签: c# asp.net gridview

我的页面中有一个GridView:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="White"
    AutoGenerateColumns="False" EmptyDataText="No data available." BorderColor="#DEDFDE"
    BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="729px" ForeColor="Black"
    GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <RowStyle BackColor="#F7F7DE" />
    <Columns>
        <asp:TemplateField HeaderText="TransactionKey" SortExpression="TransactionKey">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewTransactionKey" runat="server" Text='<%# Bind("TextBoxTransactionKey") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewTransactionKey" runat="server" Text='<%# Bind("TextBoxTransactionKey") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TerminalID" SortExpression="TerminalID">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewTerminalID" runat="server" Text='<%# Bind("TextBoxTerminalID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewTerminalID" runat="server" Text='<%# Bind("TextBoxTerminalID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MerchantID" SortExpression="MerchantID">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewMerchantID" runat="server" Text='<%# Bind("TextBoxMerchantID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewMerchantID" runat="server" Text='<%# Bind("TextBoxMerchantID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="شماره حساب" SortExpression="شماره حساب">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewBankAccount" runat="server" Text='<%# Bind("TextBoxBankAccount") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewBankAccount" runat="server" Text='<%# Bind("TextBoxBankAccount") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="نام بانک" SortExpression="نام بانک">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:CommandField ShowSelectButton="True" >
        <ItemStyle Font-Size="12px" />
        </asp:CommandField>
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

我使用以下代码设置DataSource:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        LabelTitr.Text = "Add Banks";
        LabelResult.Text = "";
        if (Session["Username"] != null)
        {
            string permission = "";
            bool login = PublicMethods.CheckUsernamePass(Session["Username"].ToString(), Session["Password"].ToString(), out permission);
            if (!login)
            {
                permission = "";
                Response.Redirect("../Default.aspx");
                Session["Username"] = Session["Password"] = null;
                return;
            }
        }
        else
        {
            Response.Redirect("../Default.aspx");
            Session["Username"] = Session["Password"] = null;
            return;
        }

        if (!IsPostBack)
            BindDataToGridView1();
    }
    catch (Exception ex)
    {
        LabelResult.Text = ex.Message;
    }
}

void BindDataToGridView1()
{
    try
    {
        DataSet1 dataSet = new DataSet1();
        DataClasses2DataContext dbc2 = new DataClasses2DataContext();
        var Definitions = dbc2.Definitions;
        if (Definitions.Count() <= 0) return;
        foreach (var Definition in Definitions)
        {
            string bankName = dbc2.Banks.Where(c => c.BankID == Definition.BankID).First().BankName;
            string CheckBox = "<input name=\"CheckBoxSubmitChanges\" type=\"checkbox\" value=" + Definition.DefinitionID + " />";
            dataSet.DataTableBanks.Rows.Add(bankName, Definition.BankAccount, Definition.MerchantID, Definition.TerminalID, Definition.TerminalID);
            GridView1.DataSource = dataSet.DataTableBanks;
        }
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        LabelResult.Text = ex.Message;
    }
}

这是GridView1_SelectedIndexChanged方法:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string temp = row.Cells[4].Text;
    LabelResult.Text = temp;
}

但总是temp string是空的!
这有什么问题?
感谢

3 个答案:

答案 0 :(得分:3)

如果它是TemplateField,则必须使用FindControl(“控件ID”)选项,而不是单元格的文本。我将单元格4视为模板字段...

答案 1 :(得分:2)

由于您的数据存储在控件中,因此没有Text。您需要搜索所需的控件并将Text拉出。

例如:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = GridView1.SelectedRow; 
    string temp = row.Cells[4].FindControl('LabelGridViewBankName').Text; 
    LabelResult.Text = temp; 
}

如果您使用DataKey属性,可以选择关闭{{1}}属性,然后使用它来搜索数据源以获取所需的值。

答案 2 :(得分:2)

//this will give you control over the textbox object
var field = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("your_control_ID"));
//and here you can access the text
string temp = field.Text;