Solved

Only show hyperlink if results return specific value ASP.NET Gridview SQL c#

Posted on 2014-10-10
9
646 Views
Last Modified: 2014-10-20
I am working on some asp and having an issue trying to get my vision working.

So I currently have a sql table that has the following columns (xrefid,requested,status)

I have the datagrid populate based on xrefid and give me all values of the requested values.

However, I am looking to make it so that the "Requested" value if a hyperlink if status ='S' and not a hyperlink if Status='N'

So it would be similar to the picture attached.

GridView want
0
Comment
Question by:desiredforsome
  • 6
  • 3
9 Comments
 
LVL 28

Assisted Solution

by:sammySeltzer
sammySeltzer earned 500 total points
ID: 40374880
Try this:

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="150" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="lnkStatusYes" NavigateUrl='<%# Eval("Status","http://www.abc.com/web/sompage.aspx?id={0}") %>'
                    Text="Status" Visible='<%# Eval("Status").ToString() == "Y" %>'
                    runat="server" />
                <asp:Label ID="lblStatusNo" Text='<%# Eval("Status") %>' Visible='<%# Eval("Status").ToString() == "N" %>'
                    runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {            
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Status",typeof(string)) });
        dt.Rows.Add(1, "Y");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Open in new window


You may have to make a few changes.

Also, you will have to handle when data is empty or it is neither Y nor N.
0
 

Author Comment

by:desiredforsome
ID: 40379862
I have this error -

Error      12      An array initializer of length '1' is expected
0
 

Author Comment

by:desiredforsome
ID: 40379870
Also trying to see how this is linked to sql
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:desiredforsome
ID: 40379917
I am getting it to work somewhat. However It is genreating two columns. One that has the descrition and one tha thas the status.

I am trying to make the description the hyperlink if the status is "N" and the description should display fromt he sql command i wrote.

SELECT requested,status from outlookreport.dbo.requestdoc.
0
 

Author Comment

by:desiredforsome
ID: 40379940
I think I got it. I must use the expression

Text='<%# Eval("Requested").ToString() %>'

This is what I got working from hyperlink. So now I just need to make it work for the label
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40380050
So, what is not working as far as the label goes?
0
 

Assisted Solution

by:desiredforsome
desiredforsome earned 0 total points
ID: 40380295
Ok So I got it working and Sammy you are the bomb. I made some corretions to your code and got it to do exactly what I wanted.

<div style="overflow-y: scroll; width: 100%; height: 280px;">
                        <asp:GridView class="Grid" ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                            runat="server" AutoGenerateColumns="False" Width="381px">
                            <Columns>

                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Image ID="imgCover" runat="server" AlternateText="Image Cover" Visible='<%# Eval("Status").ToString() == "N" %>' ImageUrl="images/checkbox1.png" />
                                        <asp:Image ID="Image1" runat="server" AlternateText="Image Cover" Visible='<%# Eval("Status").ToString() == "Y" %>' ImageUrl="images/nocheck1.png" />
                                        <asp:HyperLink ID="lnkStatusYes" NavigateUrl='<%# Eval("Status","requireddocs.aspx") %>'
                                            Text='<%# Eval("Requested").ToString() %>' Visible='<%# Eval("Status").ToString() == "Y" %>'
                                            runat="server" />
                                        <asp:Label ID="lblStatusNo" Text='<%# Eval("Requested").ToString() %>' Visible='<%# Eval("Status").ToString() == "N" %>'
                                            runat="server" />

                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns>

                            <HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
                        </asp:GridView>
                    </div>

Open in new window

 private void loadrequired()
    {
        SqlConnection sqlconnection1 = new SqlConnection("Data Source=192.168.2.121\\EMMSDE;Initial Catalog=outlookreport;User ID=jsmith;Password=s5993153492");
        SqlCommand cmd5 = sqlconnection1.CreateCommand();
        cmd5.CommandText="SELECT requested,status from outlookreport.dbo.requestdoc";
        
        
        sqlconnection1.Open();
        SqlDataReader reader2 = cmd5.ExecuteReader();
        GridView2.DataSource = reader2;
        GridView2.DataBind();
    }

Open in new window

0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 40380515
well done!
0
 

Author Closing Comment

by:desiredforsome
ID: 40391506
Sammy had a great start and with a little combination of items I was able to produce the result needed.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question