How to make a hyperlink through GridView column value? - c#

I want here that user can see the answer under the question by selecting its heading. The question should be a hyperlink to redirect me on the page Answer.aspx. I mean to say that when user take cursor over the How to do this? it should redirect the user to the desired page. How can I do that?
here is the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="100%" BorderStyle="None">
<Columns>
<asp:BoundField DataField="QuestionHEAD" HeaderText="Question"
SortExpression="QuestionHEAD" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Problem" HeaderText="Problem"
SortExpression="Problem" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
<asp:BoundField DataField="Forum" HeaderText="Forum" SortExpression="Forum" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Username" HeaderText="Asked By"
SortExpression="Username" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="SELECT [QuestionHEAD], [Problem], [Forum], [Username] FROM [Question]">
</asp:SqlDataSource>

You can use a HyperLinkField for this:
<asp:HyperLinkField DataNavigateUrlFormatString="/somepage.aspx?a={0}&b={1}" DataNavigateUrlFields="Column1, Column2" />

Try something like this
Eval("id") should be the question id and Eval("Question") should be the question field in your database
<asp:TemplateField HeaderText="Question" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Eval("Question")%>
</ItemTemplate>
</asp:TemplateField>

Related

want to update the column of specific row of gridview

I have added a buttonfield named "accept" in the gridview..I want to update the column 'status' of bitfield to '1' of that specific row when admin clicks on accept.. Im working on c#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server" Width="1000px"
AllowPaging="True" PageSize="8"
CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" DataSourceID="SqlDataSource1" AllowSorting="True">
<Columns>
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>
<asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
<asp:BoundField DataField="emailid" HeaderText="emailid" SortExpression="emailid" />
<asp:BoundField DataField="contact" HeaderText="contact" SortExpression="contact" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbrubyConnectionString3 %>" SelectCommand="SELECT seller.fname, seller.emailid, seller.contact,seller.type, login.username, login.password FROM seller INNER JOIN login ON seller.sid=login.lid where login.status= ORDER BY login.createdate ">
Add an event handler for GridView1_RowCommand, looking for the command "Update", and grab the DataItem so you know which item to update, then update it in the database, and then rebind your gridview.
You could also make an ajax call to update the value, which would be faster (less data to transmit and receive), and wouldn't require a full page refresh, but that is more work to set up.
Add an identifier (CommandArgument) to your command by changing:
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>
to:
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"
CommandArgument="<%# Eval("emailid") %>"/>
Add event handler by changing:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
DataSourceID="SqlDataSource1" AllowSorting="True">
to:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
DataSourceID="SqlDataSource1" AllowSorting="True"
OnRowCommand="GridView1_OnRowCommand">
Then write your update in the backend by adding:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch(e.CommandName)
{
case "Accept":
var email = e.CommandArgument.ToString();
Accept(email); // write this function
break;
}
}

Binding Checkbox with database column

I am binding Checkbox field to database and want to show actual value like if in database it is 1 then checkbox should be ticked in gridview else unchecked. I tried but my code doesn't show avtual value in checkbox, always remain unchecked.
CODE:
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" runat="server" AutoGenerateColumns="false" CssClass="mGrid" Width="450px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" />
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Value='<%# Eval("IsComplaint") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs
SELECT [ID]
,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 106), ' ','/') as RecievingDate
,[ToMobileNo]
,[FromMobileNo]
,[Message],
[IsComplaint]
FROM [CmsSMSDb].[dbo].[ReceivedMessages]
where Convert(date,ReceivedDateTime)>= #DateFrom AND Convert(date,ReceivedDateTime)<= #DateTo AND IsComplaint=1
If you will get only true or false then you can use the following, by adding
Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' to the CheckBox.
<asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
If you are getting null value also, then you need to do the checking and unchecking from C# code in RowDataBound event.
Try this
<asp:CheckBoxField DataField="IsComplaint" HeaderText="IsComplaint" SortExpression="IsComplaint" />
instead of using
<asp:TemplateField HeaderText="IsComplaint"> ............. </asp:TemplateField>

How to fix the width for each column in GridView?

How can I fix the width for each column in GridView. My GridView keeps extend along with data in the cell. I need it to skip to new line when it reach the right side of the cell. This is my code in .aspx file:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="Emp_ID" DataSourceID="LinqDataSource1"
AllowPaging="True" AllowSorting="True" Width="900px" HorizontalAlign="Center" >
<Columns>
<asp:BoundField DataField="Emp_ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True" SortExpression="Emp_ID"/>
<asp:BoundField DataField="Emp_Username"
HeaderText="Username" SortExpression="Emp_Username" />
<asp:BoundField DataField="Emp_Password"
HeaderText="Password" SortExpression="Emp_Password" />
<asp:BoundField DataField="Emp_Name"
HeaderText="ชื่อพนักงาน" SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_Address"
HeaderText="ที่อยู่" SortExpression="Emp_Address" />
<asp:BoundField DataField="Emp_Tel"
HeaderText="เบอร์โทรศัพท์" SortExpression="Emp_Tel" />
<asp:TemplateField HeaderText="รูปพนักงาน" SortExpression="Emp_Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Emp_Picture") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image2" runat="server" Height="70px"
ImageUrl='<%# Eval("Emp_Picture", "{0}") %>' Width="50px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Status" HeaderText="สถานะ"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="Emp_ID"
DataNavigateUrlFormatString="AdminUpdate.aspx?Emp_ID={0}" Text="Edit" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="ChkSelectAll" runat="server"
AutoPostBack="True" oncheckedchanged="ChkSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server" AutoPostBack="True" oncheckedchanged="Chk_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's my design view:
As you can see, the first 4 columns has no problem with that until column 5. It keeps extend cause all GridView extend out of the page
Set itemstyle width of every column.For example.
<asp:BoundField DataField="Emp_Address"
HeaderText="ที่อยู่" SortExpression="Emp_Address">
<ItemStyle Width="200px" HorizontalAlign="Left" />
</asp:BoundField>
Wrapping is done based on words so that any single word is not broken in two lines.
So won't work for you if your data has long string without space.
The problem is that you can't insert spaces on your own because that may change the meaning of data.
While showing in grid we show only that long string which can be accommodated in column and then add "..." (only if string is more than what is showing) and then add a tooltip to show the full string.
That way the grid formatting is not compromised and if user wants he can hover his mouse and see the whole string.
hope this helps Example
Try setting the property RowStyle-Wrap of the GridView to True.
You can use ItemStyle-Width to fix the column width ,but in case you enter characters which can not be enclosed in given width (As seen in your image) ,column will expand as per the inputs so you can use RowStyle-Wrap property of gridview
<asp:GridView ID="grdVwtrial" runat="server" RowStyle-Wrap="true">
RowStyle-Wrap==true should work I guess. For more clarifications refer this:
http://forums.asp.net/t/1263769.aspx

How to find which page in DetailsView holds selected GridView row

I am setting up HealthMonitoring and I am writing a couple of pages to manage the event logs.
I have one page which has a GridView and another that has a DetailsView. The GridView has a template button on each row with an onClick event that loads the second page with the DetailsView on it.
What I would like to do is; when I click the template button on the GridView it then loads the second page with the DetailsView and inserts that particular record from the GridView into the DetailsView table.
The DetailsView must have paging enabled. I am stuck trying to find out which page index to load with the right record from the GridView. Currently it will just load up the first page index, then I have to click next to the record I need.
Without the paging enabled, I can read my 2 global variables and then load the correct record into the DetailsView table, but with paging enabled I'm not sure how to do it.
On the GridView page:
protected void Details1_ButtonClick(object sender, EventArgs e)
{
//I set 2 global variables here of the selected EventId and
//Details to read when the next page loads
Response.Redirect("ErrorDetails.aspx");
}
On the DetailsView page.
protected void Page_Load(object sender, EventArgs e)
{
//without paging I can set the SqlDataSource1.SelectCommand to select
//the correct record using one of the global variables
}
I have tried to use DetailsView1_PageIndexChanged, DetailsView1_Load and Page_Load to get the value in the first row, but for some reason it is allways a page behind. The variable always shows the ID of the previous page when it loads. I was going to try and keep track of the page for deleting records, but it will not work either.
This is the DataSource and DetailsView in ErrorDetails.aspx:
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource1" DataKeyNames="EventId" AutoGenerateRows="False" OnItemDeleted="DetailsView1_ItemDeleted" OnItemDeleting="DetailsView1_ItemDeleting" AllowPaging="True" OnLoad="DetailsView1_Load" OnPageIndexChanged="DetailsView1_PageIndexChanged" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<AlternatingRowStyle CssClass="alt" />
<Fields>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="EventId" HeaderText="EventId" />
<asp:BoundField DataField="EventTimeUtc" HeaderText="EventTimeUtc" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" />
<asp:BoundField DataField="EventType" HeaderText="EventType" />
<asp:BoundField DataField="EventSequence" HeaderText="EventSequence" />
<asp:BoundField DataField="EventOccurrence" HeaderText="EventOccurrence" />
<asp:BoundField DataField="EventCode" HeaderText="EventCode" />
<asp:BoundField DataField="EventDetailCode" HeaderText="EventDetailCode" />
<asp:BoundField DataField="Message" HeaderText="Message" />
<asp:BoundField DataField="ApplicationPath" HeaderText="ApplicationPath" />
<asp:BoundField DataField="ApplicationVirtualPath" HeaderText="ApplicationVirtualPath" />
<asp:BoundField DataField="MachineName" HeaderText="MachineName" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" />
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:Label ID="Label1" runat="server">
<%= EventVariables.EventDetails %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Fields>
<PagerSettings Mode="NextPreviousFirstLast" />
<PagerStyle CssClass="pager" />
</asp:DetailsView>
This is the DataSource and GirdView from the parent form:
<asp:GridView
ID="ErrorGrid"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
DataKeyNames="EventId"
OnRowDeleting="ErrorGrid_RowDeleting"
AllowPaging="True"
AllowSorting="True"
Font-Size="Small"
CellPadding="10"
CellSpacing="1" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Details1" Text="Details" runat="server" AutoPostBack="true" OnClick="Details1_ButtonClick" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" SortExpression="EventTime" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" SortExpression="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" SortExpression="ExceptionType" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT * FROM [aspnet_WebEvent_Events]"
DeleteCommand="DELETE FROM [aspnet_WebEvent_Events] WHERE [EventId]=#EventId">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>
I managed to solve this by doing a calculation. As there is 10 rows per page in the GridView I'm able to calculate the page by doing this:
EventVariables.EventPage = ((((ErrorGrid.PageIndex + 1) * 10) - 10) + Row.RowIndex);
And then loading the page in the DetailsView from the variable when it loads.

Is there a way to make asp.net Boundfield a link?

Im sorry if this has been asked before, but i have searched the forums for quite some time and have not been able to find a solution to my problem.
I am currently developing a web application where users are able to donate bottle deposit. When users have uploaded the bottles they will be shown in the following table:
Snippet of the table
The data is shown in a gridview with a SqlDataSource. The code:
<div class="table-responsive col-8">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [creator], [amount], [details],
[imageurl], [pickup], [available_from] FROM [deposit] ORDER BY [id] DESC"></asp:SqlDataSource>
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
I would like to be able to click on each individual post and create dynamic pages where more data from the database is shown. I read somewhere that i was possible to create these page with the ID of each post but i couldnt find anything that showed how it works.
Be free to ask any follow questions. This is my first post so im not sure if i have covered everything.
Thanks a lot in advance.
One way to do this is with a link in each row. First, you need to add "id" to the SELECT in your DataSource, so the data is accessible at the time the table is bound.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [id], [creator], [amount], [details], [imageurl],
[pickup], [available_from] FROM [deposit] ORDER BY [id] DESC">
</asp:SqlDataSource>
Then in your GridView, make a template column with a HyperLink, and have the NavigateUrl of that HyperLink open a new page with the ID on the query string. If you don't want the ID to be exposed in the URL, you may need to obfuscate it in some way, or use perhaps a GUID column in the DataBase for each record. As long as you can identify it. Your NavigateUrl can be whichever page you want, but in this example I used DetailPage.aspx.
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="DetailsLink" runat="server"
NavigateUrl="DetailPage.aspx?id=<%# Eval("id")%>"
Text="Details" Target="_blank">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
The <## Eval('id')#> will render the ID for each row as a query string in the URL.
If I understand you well, You can use Template Field
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>

Categories

Resources