Changing Hyperlink text in VB code for Gridview - vb.net

In the VB code below, I'm trying to set the Hyperlink hypNote's text as "Add Note" when text is null. However it is not working. As a test, I've even set the Hyperlink text as "Test" and tried:
If hypNote.text = "Test" Then
hypNote.text = "Add Note"
End If
But still it doesn't work. Here's my code..
<asp:Panel ID="pnlOrders" runat="server">
<asp:Image ID="LiveOrders" alt="Live Gif" runat="server" class="extrasButton" ImageUrl="~/files/images/liveOrders.gif" />
<asp:SqlDataSource ID="DSOrders" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT tblOrders.OrderID, tblOrders.stallmessage, tblOrders.price, tblAccounts.city, tblAccounts.postcode, tblOrders.phoneNo, tblOrders.tblNo, tblOrders.info, tblOrders.orderDate, tblOrders.orderStatus, tblOrders.type, tblOrders.timeFor, tblOrders.paid, tblOrders.tblNo
FROM tblOrders INNER JOIN tblAccounts ON tblOrders.accountID = tblAccounts.AccountID
WHERE tblOrders.orderStatus='Completed'
ORDER BY tblOrders.timeFor ASC">
</asp:SqlDataSource>
<asp:GridView ID="gdvOrders" width="100%" runat="server" style="font-size:1.5em" ShowHeaderWhenEmpty="True" EmptyDataText="No orders" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="orderID" DataSourceID="DSOrders" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("stallMessage") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:BoundField DataField="phoneNo" HeaderText="Phone No" />
<asp:TemplateField HeaderText="Address/Table No.">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("tblNo") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-ForeColor="Red" HeaderText="Note">
<ItemTemplate>
<asp:HyperLink ID="hypNote" style="Font-Size:20px; color:Red;" runat="server" NavigateUrl='<%# "~/editNote.aspx?note=" & Eval("info").ToString & "&orderID=" & Eval("orderID").ToString %>' ><%# Eval("info") %></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="type" HeaderText="Type" />
<asp:BoundField DataField="orderDate" DataFormatString="{0: H:mm:ss}" HeaderText="Order Time" SortExpression="orderDate" />
<asp:BoundField DataField="timeFor" DataFormatString="{0: H:mm:ss}" HeaderText="Time For" SortExpression="timeFor" />
<asp:BoundField DataField="paid" HeaderText="Paid" />
<asp:TemplateField ShowHeader="True">
<ItemTemplate>
<asp:ImageButton visible="true" ID="lnkSent" runat="server" CausesValidation="False"
onclientclick="return confirm('Mark As Sent?');"
ImageUrl="~/files/images/icons/sendIcon.png" onclick="lnkSent_Click"></asp:ImageButton>
<asp:HiddenField ID="hidnOrderID" runat="server" Value='<%# Eval("orderID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
Protected Sub gdvOrders_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvOrders.RowDataBound
For Each r As GridViewRow in gdvOrders.Rows
If r.RowType = DataControlRowType.DataRow Then
Dim hypNote As Hyperlink
hypNote = r.Cells(4).FindControl("hypNote")
If hypNote.text = "" Then
hypNote.text = "Add Note"
End If
End If
Next r
End Sub
Additionally, I wish to change the text color of the Hyperlink when "Add Note" is displayed.

I figured out my mistake. I changed this line to:
<asp:HyperLink ID="hypNote" style="Font-Size:20px; color:Red;" text='<%# Eval("info") %>' runat="server" NavigateUrl='<%# "~/editNote.aspx?note=" & Eval("info").ToString & "&orderID=" & Eval("orderID").ToString %>' ></asp:HyperLink>
I didn't have text= in there.
All worked fine then.

Related

Exclude column in certain rows - SQL / Asp.Net

I'm wanting to change some code which was written for me. I would like to omit displaying the hyperlink where tblResults.videoLink=''
The hyperlink is displayed in the table as "Video Link" so people can click on it. I only want the words/hyperlink "Video Link" to appear in the row where people have entered one.
The code is Microsoft SQL but is written in the ASPX file. The back end is VB but there's VB is pretty much empty.. The ASPX file handles everything...
<h1>Recent Results</h1>
<asp:Panel ID="pnlMyWishes" runat="server">
<asp:SqlDataSource ID="DSWishes" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT tblResults.wishID, tblResults.Player1AccountID, tblResults.Player2AccountID, tblResults.Player1Result, tblResults.Player2Result, tblResults.venue, tblResults.potSize, tblResults.player1Name, tblResults.player2Name, tblResults.videoLink, tblResults.date FROM tblResults ORDER BY tblResults.date DESC">
</asp:SqlDataSource>
<asp:GridView ID="gdvWishes" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="wishID" DataSourceID="DSWishes" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:BoundField DataField="player1Name" HeaderText="Player 1" />
<asp:TemplateField HeaderText="Result">
<ItemTemplate>
<asp:Label ID="lblP1" Text='<%# Eval("player1Result").ToString %>' runat="server" Visible="true">
</asp:Label><asp:Label ID="lblVs" Text=" - " runat="server" Visible="true"></asp:Label>
<asp:Label ID="lblP2" Text='<%# Eval("player2Result").ToString %>' runat="server" Visible="true"></asp:Label>
</ItemTemplate>
<ItemStyle Width="17%" />
</asp:TemplateField>
<asp:BoundField DataField="player2Name" HeaderText="Player 2" />
<asp:BoundField DataField="potSize" HeaderText="Pot" />
<asp:BoundField DataField="venue" HeaderText="Venue" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HyperLink ID="hypVideoLink" runat="server" NavigateUrl='<%# Eval("videoLink").ToString %>'>Video Link</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
Create a sub like below, call it after the code that fills your GridView with data.
Private Sub HideBlankURLs()
For Each r As GridViewRow in gdvWishes.Rows
If r.RowType = DataControlRowType.DataRow Then //Execute the code only for datarow, excluding footer and header
Dim hypURL As HyperLink
hypURL = r.Cells(5).FindControl("hypVideoLink") //Goes to the column of VideoURL, index 5 is counting from 0 to 5
If hypURL.NavigateURL = "" Then //Checks if the URL is blank, if it is then hide the hyperlink control
hypURL.Visible = False
End If
End If
Next r
End Sub
This will loop through all the rows and look for the control in that column, check if navigate URL is blank, then hide that control.
NOTE: Replace the (//) with single quote (') because SO reads it as text enclosing character rather than a comment.

get Sum of values in column from sqlDataSource

I have a SqlDataSource dsDetails that selects from the DB table.
<asp:SqlDataSource ID="dsDetails" runat="server" SelectCommand="spGetDetails"
OnSelected="dsDetails_Selected" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="mode" QueryStringField="mode" Type="String" DefaultValue="NULL" />
</SelectParameters>
</asp:SqlDataSource>
The result is bound to gridview in the aspx page
<asp:GridView CssClass="content" ID="gridDetails" runat="server" DataSourceID="dsDetails" AllowSorting="True" AllowPaging="True" PagerSettings-Position ="TopAndBottom" DataKeyNames="ID" AutoGenerateColumns="False" >
<PagerSettings Position="TopAndBottom" Mode="NumericFirstLast" />
<EmptyDataTemplate>
<b>There are no records to display.</b>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="empNum" HeaderText="Employee Number" SortExpression="empnum" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empName" HeaderText="Emplopyee Name" SortExpression="empname" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empDoj" HeaderText="Emplopyee Date of joining" SortExpression="empdoj" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Salary" SortExpression="sal">
<EditItemTemplate>
<asp:TextBox ID="txtSal" runat="server" Text='<%# Eval("sal", "{0:F}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("sal", "{0:C2}") %>'></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="btnSave" runat="server" CausesValidation="True" CommandName="cmdSave"
Text="Save" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="cmdCancel"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" CausesValidation="False" CommandName="cmdApprove"
Text="Approve" />
<asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="cmdEdit"
Text="Edit" />
<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CommandName="cmdDelete"
Text="Delete" />
<ajaxToolkit:ConfirmButtonExtender ID="cbeDelete" runat="server"
TargetControlID="btnDelete"
ConfirmText="Are you sure?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now what i have to achieve is Find the Sum of the Salary Column of the grid view. How to achieve this using the Data Source in the page load method? I have to get the sum of Salary for the entire result set (avg records returned 60-80)
Iterating through the rows and computing the sum in gridDetails_RowDataBound wont be effective since the rows.count will be max of 10 since the page size is 10. When the result set has more than 10 records say 55, It will compute the sum for the current gridview page alone (first 10 records). I have to get the sum for all 55 records and display it in a label in the pageload method.
Please provide suggestion..
The SQLDataSource provides a Selected event that fires after data retrieval has finished. See SqlDataSource.Selected Event. You should be able to do a sum on the rows in the grid in this event.
This worked for me
In PageLoad method add
dvSql = DirectCast(dsDetails.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
amtSum += CDbl(drvSql("sal"))
Next
lblTotalAmt.Text = CStr(recoveryAmtSum)
In dsDetails_Selected method add this line
e.Command.Parameters("#approvaltype").Value = mode

GridView SelectParameter from code behind

I am using VB.NET webforms.
I have a GridView which brings data from Database when the page loads. The SQLDataSource with Select SQL is as follows:
<asp:SqlDataSource
ID="ScopeDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:SQLServerConnectionString %>"
SelectCommand="SELECT [SCO_LINENUM], [SCO_LINETEXT], [SCO_SCOPEID], [SCO_TAB1LINK], [SCO_TAB2LINK], [SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = 'AAAREN02'"></asp:SqlDataSource>
My GridView is as follows:
<asp:GridView
ID="ScopeGrid"
runat="server"
DataSourceID="ScopeDataSource"
AutoGenerateColumns="False"
GridLines="None"
DataKeyNames="SCO_SCOPEID, SCO_LINENUM">
<Columns>
<asp:BoundField DataField="SCO_LINENUM" HeaderText="Sr#" SortExpression="SCO_LINENUM">
<HeaderStyle Height="40px" />
<ItemStyle Width="30px" />
</asp:BoundField>
<asp:BoundField DataField="SCO_LINETEXT" HeaderText="Parameter.." SortExpression="SCO_LINETEXT">
<HeaderStyle Height="40px" />
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Operator Values...........">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="DropDownMargin">
<asp:ListItem>BETWEEN</asp:ListItem>
<asp:ListItem>EQUALTO</asp:ListItem>
<asp:ListItem>GREATERTHAN</asp:ListItem>
<asp:ListItem>LESSTHAN</asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="RangeStart" Text="None" Width="50px" CssClass="TextBoxMargin"></asp:TextBox>
<asp:TextBox runat="server" ID="RangeEnd" Text="None" Width="50px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Height="40px" CssClass="WhiteSpacePreserve" />
</asp:TemplateField>
<asp:BoundField DataField="SCO_SCOPEID" HeaderText="ScopeID" SortExpression="SCO_SCOPEID">
<HeaderStyle CssClass="hidegrid" />
<ItemStyle CssClass="hidegrid" />
</asp:BoundField>
<asp:BoundField DataField="SCO_TAB1LINK" HeaderText="Tab1" SortExpression="SCO_TAB1LINK" />
<asp:BoundField DataField="SCO_TAB2LINK" HeaderText="Tab2" SortExpression="SCO_TAB2LINK" />
<asp:BoundField DataField="SCO_TAB3LINK" HeaderText="Tab3" SortExpression="SCO_TAB3LINK" />
</Columns>
</asp:GridView>
As seen in above SQLDataSource, the WHERE clause is hard-coded. I want to make this dynamic by passing a variable to the WHERE clause from code-behind. Also My Page_Load event is empty. So far, since the SQLDataSource WHERE clause is hard-coded in my aspx I can see the data in gridview but I don't know how to send a variable to it from code-behind?
Something like this would work.
Change your select command to
SELECT [SCO_LINENUM], [SCO_LINETEXT], [SCO_SCOPEID], [SCO_TAB1LINK], [SCO_TAB2LINK], [SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = #SCO_SCOPEID
Then do like
ScopeDataSource.SelectParameters("SCO_SCOPEID").DefaultValue = Your value

Why distinct() is not working in my sqldatasource?

enter image description hereI want to display timetable to faculty i.e they will have different batch allocated to them with same date but i want to display in single grid view.I have tried with distinct but its not working.They will have first hour and second hour with different batch and i want to display there subject and batch in single column but its showing in two column in single grid view. please help me out from these problem
<label>From Date</label>
<asp:TextBox ID="fromdate" runat="server" TextMode="Date"></asp:TextBox>
<label>To Date</label>
<asp:TextBox ID="todate" runat="server" TextMode="Date"></asp:TextBox>
<asp:Button ID="viewid" runat="server" OnClick="viewid_Click" Text="View" CssClass="button2"/>
<asp:GridView ID="Viewsubjects" runat="server" AutoGenerateColumns="False" DataSourceID="batch1" CssClass="mGrid1">
<Columns>
<asp:BoundField DataField="datedif" HeaderText="datedif" SortExpression="datedif" />
<asp:TemplateField HeaderText="Hour1" SortExpression="subject1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Visible='<%# Eval("subject1")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject1")%>'></asp:Label>
<br />
<asp:Label ID="label6" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject1")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour2" SortExpression="subject2">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Visible='<%# Eval("subject2")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject2")%>'></asp:Label>
<br />
<asp:Label ID="label7" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject2")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour3" SortExpression="subject3">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Visible='<%# Eval("subject3")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject3")%>'></asp:Label>
<br />
<asp:Label ID="label8" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject3")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour4" SortExpression="subject4">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Visible='<%# Eval("subject4")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject4")%>'></asp:Label>
<br />
<asp:Label ID="label9" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject4")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour5" SortExpression="subject5">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Visible='<%# Eval("subject5")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject5")%>'></asp:Label>
<br />
<asp:Label ID="label10" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject5")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="batch1" runat="server"
ConnectionString="<%$ ConnectionStrings:Database1ConnectionString1 %>"
SelectCommand="SELECT DISTINCT [datedif], [subject1], [subject2], [subject3], [subject4], [subject5],[batch] FROM [test] WHERE ([datedif] >= #datedif)">
<SelectParameters>
<asp:ControlParameter ControlID="fromdate" Name="datedif" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
These is how am getting the output
Try these queries given below-
create table timetable1(date date,hour1 int,subject1 varchar(255),batch1 varchar(20),hour2 int,subject2 varchar(255),batch2 varchar(20),hour3 int,subject3 varchar(255),batch3 varchar(20),faculty varchar(255));
insert into timetable1 values(cast(getdate() as date), 1, 'subject1','B1',0, 's2','b2',3,'s3','b3','ms. hart');
insert into timetable1 values(cast(getdate() as date), 1, 'subject12','B12',0, 's12','b12',3,'s13','b13','ms. hart11');
insert into timetable1 values(cast(getdate() as date), 1, 'subject13','B122',0, 's222','b222',3,'s23','b23','ms. hart22');
select date,(subject1+' '+batch1) as hour1, (subject2+' '+batch2) as hour2,(subject3+' '+batch3) as hour3,faculty from timetable1group by date, faculty,subject1,subject2,subject3,batch1,batch2,batch3
What you're getting IS actually distinct, because each row has different values. What you're looking for is a "group by", which will group your values by day and faculty. My solution is based on replacing your current solution of:
date hour1 subject1 hour2 subject2 hour3 subject3 faculty
2015-06-25 1 subject 1 0 (null) 0 (null) ms. hart
2015-06-25 0 (null) 1 subject 2 0 (null) ms. hart
2015-06-25 0 (null) 0 (null) 1 subject 3 ms. hart
with:
date hour1 hour2 hour3 faculty
|----------|----------|-----------|-----------|----------|
2015-06-25 subject 1 subject 3 subject 3 ms. hart
If this is assumption is correct, then here is the query I used (I created my own tables because you did not provide DDL but if you have issues let me know)
select date,max(subject1) hour1, max(subject3) hour2, max(subject3) hour3, faculty from timetable
group by date, faculty
And here is the sql fiddle for you to experiment :)
http://sqlfiddle.com/#!6/c1850/3

Linkbutton in a Datalist in a GridView

I have an interesting ASP/VB.NET problem. I have a gridview where each row has its own datalist in a template column. I want to add to each item in the datalist a linkbutton that will trigger an event based on data in the datalist. But I'm not sure how to do it. It's in a project so it has the designer file which does list the gridview but not the datalist inside the gridview. When I try to add it, the listing for the datalist is removed later on when I compile.
My question is now do I get the linkbutton in the datalist in the gridview to do something?
<asp:GridView ID="gvCmteNom" runat="server" AutoGenerateColumns="False" showheader="true" HeaderStyle-BackColor="Silver" Width="1600px">
<Columns>
<asp:TemplateField HeaderText="CURRENT SERVICE">
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%# Bind("NOMINEE_ID") %>' Visible="False" Width="25px" />
<asp:DataList ID="dlCurrentCmtes" runat="server" DataSourceID="dsCurrentCmte" RepeatLayout="Flow" DataKeyField="ID" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdnUserID" Value='<%# Eval("ID") %>' />
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' />
<asp:LinkButton runat="server" ID="lbIncrementYear" CommandName="IncrementYear" CommandArgument='<%# Eval("ProductID") %>' Text="Add Year" />
</ItemTemplate>
<SeparatorTemplate><br /><br /></SeparatorTemplate>
</asp:DataList>
<asp:SqlDataSource ID="dsCurrentCmte" runat="server"
ConnectionString=""
ProviderName="System.Data.SqlClient" SelectCommand="spCmteList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtID" Name="ID" PropertyName="Text" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Two options:
Option 1
Specify the ItemCommand event handler for the DataList and use that to respond to the event:
<asp:DataList OnItemCommand="Item_Command" />
Then specify that function in the code behind:
Sub Item_Command(sender As Object, e As DataListCommandEventArgs)
If e.CommandName = "IncrementYear" Then
...
End If
End Sub
Option 2 Specify the OnClick eventhandler for the link button
<asp:LinkButton OnClick="LinkButton_Click" runat="server"/>
Then
Sub LinkButton_Click(sender As Object, e As EventArgs)
...
End Sub