views:

314

answers:

1

Hi,

as the title says, How do I disable the select button text in a gridview after clicking it once? I want to click it once, then have the select cell area render an image (and the image not clickable or linking to anything).

Any ideas?

protected void Page_Load(object sender, EventArgs e)
{
    dn = new holdDataContext();
    if (!(Page.IsPostBack))
    {

        //    GridView1.DataSource = dn.tennis.ToList();
        //  GridView1.DataBind();
        GridView1.DataSource = from c in dn.tennis
                               orderby c.ID descending
                               select c;

        GridView1.DataBind();
    }
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataSet ds = null;
    if (Session["oro"] == null)
    {
        ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Name"));
        dt.Columns.Add(new DataColumn("Description"));
        ds.Tables.Add(dt);
        Session["oro"] = ds;
    }
    else
    {
        ds = (DataSet)Session["oro"];
    }
    DataRow row = ds.Tables[0].NewRow();
    row["Name"] = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
    row["Description"] = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
    ds.Tables[0].Rows.Add(row);

}
A: 

you'll need a combination of aspx markup and code-behind:

aspx:

<asp:GridView ID="gvSample" runat="server" 
        DataKeyNames="CustomerID" 
        onselectedindexchanged="gvSample_SelectedIndexChanged">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" Text="Select"></asp:LinkButton>
                    <asp:Image ID="imgSelect" runat="server" ImageUrl="~/imgs/whatever.jpg" Visible="false" />
                </ItemTemplate>

code-behind:

protected void gvSample_SelectedIndexChanged(object sender, EventArgs e) {
    LinkButton linkButton = gvSample.SelectedRow.Cells[0].FindControl("btnSelect") as LinkButton;
    Image imgWhatever = gvSample.SelectedRow.Cells[0].FindControl("imgSelect") as Image;
    linkButton.Enabled = false;
    linkButton.Visible = false;
    imgWhatever.Visible = true;
}

so, in the ItemTemplate markup of the GridView, specify the image you want to replace the Select button with but make it invisible, then disable the Select button in place of the image by swapping visibility between both objects within the event handler method gvSample_SelectedIndexChanged in the code-behind, which is what gets triggered upon clicking Select button. Since FindControl returns objects of type Control you'll have to cast to the LinkButton type of your Select button.

hotroddiameter