Solved

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

Posted on 2014-10-10
9
680 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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