locked
GridView personalizing Hyperlink in C# ASP NET RRS feed

  • Question

  • User1151703306 posted

    Hi,

    On my GridView in C# ASP NET I have set this Hyperlink

    <asp:HyperLink ID="hlimg" runat="server"
               ImageUrl='<%# (!String.IsNullOrEmpty(Eval("ppt").ToString()) ? "/img/download.gif" : "/img/cross-button.png") %>'
               NavigateUrl='<%# (!String.IsNullOrEmpty(Eval("ppt").ToString()) ? String.Format(Eval("ppt").ToString()) : "") %>'
               ToolTip='<%#(!String.IsNullOrEmpty(Eval("ppt").ToString()) ? "Download Powerpoint" : "No Powerpoint")%>'
               Enabled='<%#(!String.IsNullOrEmpty(Eval("ppt").ToString()) ? true : false)%>'
               OnClick="return confirm('Confirm ?');">                                
    </asp:HyperLink>

    If on the database the field "ppt" is null or empty the return on the row is

    else

    Now I need this new output if on the database the field "ppt" is populated

    the cross-button.png should delete the attached powerpoint whose link is stored in the database table, then delete this link.

    How to do resolve this?

    Thanks in advance for any help.

    Friday, January 29, 2021 4:24 PM

Answers

  • User-1716253493 posted

    Add another control, linkbutton control

    Set commandname="deleteppt", commandargument bind to id value

    Now use gridview rowcommand to update "ppt" column to null based id from commandargument value. Then rebind the grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2021 8:57 AM

All replies

  • User-1716253493 posted

    You need more IF ELSE ?:

    logic1 ? "result1" : (logic2 ? "result2" : "result3")

    I recomend to use RowDataBound event and findcontrols instead because the code become very long, will be easier to write it in RowDataBound event 

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink hlimg = (HyperLink)e.Row.FindControl("hlimg");
                LinkButton lbdelete = (LinkButton)e.Row.FindControl("lbdelete");
                if (DataBinder.Eval(e.Row.DataItem,"ppt")!=null)
                {
                    string ppt = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ppt"));
    
                    lbdelete.Visible = false;
                    hlimg.Visible = true;
                }
                {
                    lbdelete.Visible = true;
                    hlimg.Visible = false;
                }
            }
        }

    Complete the codes

    Saturday, January 30, 2021 2:35 AM
  • User1151703306 posted

    thanks for reply, but I think I have not explained myself

    I have tried this code on markup aspx page

    ImageUrl='<%# (!String.IsNullOrEmpty(Eval("ppt").ToString()) ? "/img/download.gif" : (!String.IsNullOrEmpty(Eval("ppt").ToString())) ? "/img/cross-button.png" : "/aspnet/img/cross-button.png") %>'

    and the return is

    because the value on the database the field "ppt" is populated

    i need to place side by side the cross-button.png image to the download.gif image on ImageUrl attribute on HyperLink

    in this mode

    this new cross-button.png should delete the attached powerpoint whose link is stored in the database table, then delete this link.

    Saturday, January 30, 2021 7:38 AM
  • User-1716253493 posted

    Add another control, linkbutton control

    Set commandname="deleteppt", commandargument bind to id value

    Now use gridview rowcommand to update "ppt" column to null based id from commandargument value. Then rebind the grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2021 8:57 AM
  • User1151703306 posted

    thanks for reply.

    Where I add another control, linkbutton control ?

    it is not clear...

    Saturday, January 30, 2021 9:34 AM