简体   繁体   English

在GridView中无法获取链接按钮的文本

[英]Cannot get the text of the Linkbutton in GridView

This is how I am trying to get the text of the LinkButton:- 这就是我试图获取LinkBut​​ton文本的方式:-

protected void download(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    String name = btn.Text;
    conn.Open();
    SqlDataAdapter sda = new SqlDataAdapter("select str_file from soham_movies where title='" + name + "'", conn);
    conn.Close();
    DataTable dt = new DataTable();
    sda.Fill(dt);
    string filePath = "";
    filePath = "~/files/" + dt.Rows[0][0].ToString();

    Response.Clear();
    Response.ContentType = "movieFiles";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + dt.Rows[0][0].ToString());
    Response.WriteFile(filePath);
    Response.End();
}

But I am getting the text as "" (blank). 但是我将文本显示为“”(空白)。

Here's my aspx part:- 这是我的aspx部分:-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BorderStyle="None" AlternatingRowStyle-BorderStyle="None" CellPadding="5" 
    EditRowStyle-BorderStyle="None" HeaderStyle-BorderStyle="None" 
    PagerStyle-BorderStyle="None" RowStyle-BorderStyle="None" ShowHeader="False" 
    GridLines="None" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img id="image" src='<%# Eval("pic") %>' runat="server" style="height:120px; width:150px" />
            </ItemTemplate>                   
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="click_download" runat="server" OnClick="download"><%# Eval("title") %></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The LinkButton's text is visible in the page inside gridView and can be clicked but I face the error of the query not retrieving any result because queried name is blank LinkBut​​ton的文本在gridView内的页面中可见,可以单击,但是我遇到查询错误,因为查询名称为空而无法检索任何结果的错误

I think that due the fact that you are setting the text of the button as follows: 我认为由于您将按钮的文本设置如下:

<asp:LinkButton ID="click_download" runat="server" OnClick="download"><%# Eval("title") %></asp:LinkButton>

The Text property is not being set correctly. Text属性设置不正确。 Move the <%# Eval("title") %> into the declaration of link button and assign it's value to the Text property: <%# Eval("title") %>移至链接的声明按钮,并将其值分配给Text属性:

<asp:LinkButton ID="click_download" runat="server" OnClick="download" Text='<%# DataBinder.Eval (Container.DataItem, "title") %>'></asp:LinkButton>

I don't see where you are setting the text property/attribute for the LinkButton. 我看不到您在哪里为LinkBut​​ton设置文本属性/属性。 However, I do see where you have "<%# Eval("title") %>" floating in your tag. 但是,我的确看到标签中有“ <%#Eval(” title“)%>”浮动的地方。 Should it say Text="<%# Eval("title") %>". 它应该说Text =“ <%#Eval(” title“)%>”。

I really don't understand how it is being viewed if it's not set. 我真的不明白,如果未设置如何查看它。 Are you setting it in the Page_Load? 您是否要在Page_Load中设置它? Hopefully these questions help chase down the problem. 希望这些问题有助于解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM