Link Button to Gridview - c#

I have a following GridView and my source code look like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ShopData.ascx.cs" Inherits="usercontrols.UsercontrolsShopData" %>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Export to csv</asp:LinkButton>
<br /> <br />
<asp:GridView ID="gridviewShopData" runat="server" DataSourceID="SqlDataSource1" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT firstName as 'First Name',lastName as 'Last Name',employeeNumber as 'Employee Number',productID as 'Product Number' FROM ShopOrder where shopID=#pageid">
<SelectParameters><asp:QueryStringParameter Name="pageid" QueryStringField="id"/></SelectParameters>
</asp:SqlDataSource>
I want to make the first column values to link button that link to a url..how can i make this?

In your gridview, set AutoGenerateColumn="False". NExt, open the properties of the Gridview and convert the column to a template field. Within item template, add the hyperlink control. From here you can change the hyperlink control as you like. To access the data from the column you can use eval (eg Text='<%# Eval("ColumnName", "Run") %>'

This can be done using a TemplateField. Please example code below.
<asp:TemplateField HeaderText="Your Header Here">
<ItemTemplate>
<asp:HyperLink runat="server" Text="<%# Bind(Text) %>" NavigateUrl='<%# "Your Link Here" %>' Target="_blank"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Take template field for first column in your Grid view,and in that template field place Link button..
click on Link button->Link button Tasks->edit Data Bindings->Text=<%Eval("Your ColumnName")%

Related

avoid reload gridview c# after clicking on a button

I created a web page Default6.aspx containing a gridview.
This gridview contains w column "radio button" (just one radio button can be selected)
I have a button that when I click,the Id of the row checked will be shown.
The problem that after click on the button,the checked radio button becomes unchecked and the gridview loads new data.
I couldn't really understand why.
Note: I have the same code in another Default5.aspx and it works.
this is the code of gridview:
<div style ="height:250px; width:1092px; overflow:auto;" spellcheck="True">
<asp:GridView ID="GridView2" runat="server"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" CellSpacing="4" GridLines="Vertical" Height="160px"
OnSelectedIndexChanged="GridView2_SelectedIndexChanged" Width="271px" ForeColor="Black"
ShowHeaderWhenEmpty="True" AutoPostBack="false">
<AlternatingRowStyle BackColor="White" />
<Columns >
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Main" >
<ItemTemplate>
<asp:RadioButton ID ="radioButton1" HeaderText="Main" runat="server"
onclick = "RadioCheck(this);" AutoPostBack ="False" Checked="False" CausesValidation="True" />
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("id")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#80FF00" Font-Bold="True" ForeColor="Black" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#CCCCCC" BorderStyle="Solid" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</div>
and this is the code of the button:
<asp:Button ID="Button1" runat="server" BackColor="#80FF00" Font-Size="Large" OnClick="Button1_Click" Text="Delete unselected rows" Width="200px" />
for the test I created another button that just shows me a message "hello" when I click on it..and when I did (even I don't check a radio button),the gridview became refreshed..
what can be that?
thank you
Consider wrapping your button and the label in an updatepanel control, which uses AJAX to refresh their contents. The rest of the page will not be reloaded and the action will not affect the browser navigation.
the answer is:
if (!IsPostBack)
{ //my code}
else
{ //nothing to do}

can't access Textbox value which is in gridview control on button click

I have taken 3 text-boxes in grid view. I want to access all the 3 text-boxes value in single label on button click event.
ASP code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
CssClass="style1" ForeColor="#333333" GridLines="None" Height="342px" Width="548px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField ApplyFormatInEditMode="True" DataField="IName" HeaderText="Item Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="txtqty" runat="server" AutoPostBack="false" Text="Enter Quantity" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtqtys" runat="server" AutoPostBack="false" OnTextChanged="TextBox_Changed"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="chkAll" runat="server" onclick="checkAll(this);" AutoPostBack="true"
OnCheckedChanged="CheckBox_CheckChanged" Text="Select" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick="Check_Click(this)" AutoPostBack="True"
OnCheckedChanged="CheckBox_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
C# code:
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbltxtqty.Visible = true;
lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
}
}
so please tell me the write code for it.
You are "Contradicting" your own aspx and code behind code. Why I am saying this is because, check you Label Mark-up & button click event handler:-
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>
Here you are setting labels Visible property to true which is of no use because its by default true. Now, see your button click event you are trying to make it visible by using lbltxtqty.Visible = true;, So obviously your mark-up should first look like this:-
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="false"></asp:Label>
Because, you must be needing that when Page loads.
Now, comes your button click event. Do you know the Page Life Cycle of ASP.NET? When button click event is fired that's an obvious PostBack request so what is the Point of checking IsPostBack property there? Remove this code and it should work. Change your button click handler as follows:-
protected void btnsubmit_Click(object sender, EventArgs e)
{
lbltxtqty.Visible = true;
lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
}

editing the same cell in the gridview

hello i am creating a shopping application , I have a shopping cart in that i have some columns which are add from the code behind and some columns are of template fields which are not present in the database but want use in future.
this is my shopping cart
code :
<asp:GridView ID="GridViewProduct" runat="server" CellPadding="5" CssClass="grid"
ForeColor="#333333" OnRowCreated="GridViewProduct_RowCreated" OnRowDataBound="GridViewProduct_RowDataBound"
ShowFooter="True" CellSpacing="1"
onrowupdating="GridViewProduct_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField Text="Remove From Cart" CommandName="RemoveFromCart" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextBoxQuantity" runat="server" Width="50px">1</asp:TextBox><br />
<asp:LinkButton ID="LinkButtonEditQuantity" runat="server" CommandName="EditTextBox">edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButtonUpdate" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<asp:Label ID="LabelTotalPrice" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
i have a edit link button in the template field and the update link button in the editItem template and textbox above it so as to edit the textbox when a person clicks the edit link button.
My Question is that : How can i edit the textbox in the same column instead of whole row.
and which method should i use of the to achieve to editing the same cell rowupdating,rowcommand and save the values in session and multiply the edited value as quantity and unit price to show in the total price rows of the gridview .
please help me to implement right way to implement this as i am stuck with this for many days.. please
on your gridview templates, look at the EditRowTemplate, you will see that it contains all the editable controls (most likely textboxes) delete the textboxes and add Labels instead and bind them to your datasource. That way the data is still displayed but wont be editable (except for the control that you want to be edited)
Try it and let me know if you need more help.

Freeze Title Header in gridview asp.net c#

I have a Title in my webpage, some texts,a textbox and a button. I have a large gridview that needs to be scrolled vertically and horizontally. What i want is, Even if I scroll the gridview, the Title in my webpage, some texts,a textbox and a button will still be in the same position or freeze.
here is my html code
<div>
Data Warehouse Calendar Maintenance
<br />
<br />
</div>
<p>
<asp:Label ID="Label1" runat="server" Text="Enter Date:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btn_View" runat="server" onclick="btn_View_Click" Text="View" />
</p>
<p>
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="5" Font-Names="Calibri"
Font-Size="Medium" Font-Underline="False" ForeColor="Black"
onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="271px">
<RowStyle Wrap="False" />
<EmptyDataRowStyle Wrap="False" />
<FooterStyle BackColor="Tan" BorderColor="Black" BorderStyle="Solid"
Wrap="False" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" Wrap="False" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"
Wrap="False"/>
<HeaderStyle BackColor="Tan" BorderStyle="Solid" Font-Bold="True"
Wrap="False" />
<EditRowStyle Wrap="False" />
<AlternatingRowStyle BackColor="PaleGoldenrod" Wrap="False" />
</asp:GridView>
</p>
Yes, You can do it very easily by using css. css has the property for postion. So set your postion fixed like this.
.Title
{
Position:Fixed;
Top:120px; // Set as per your requirement
Left:120px; // Set as per your requirement
}
apply title class to that Title div.
Same can be written for texts, Textbox and for button.
Refer this sample http://www.aspsnippets.com/Articles/Scrollable-GridView-with-Fixed-Headers-in-ASP.Net.aspx
It will be useful to your problem.

How to Bind Data in HTML Drop Down List

Can any one please tell me How to Bind Data from Sql DataBase to
DropDown List(Select Drop Down) using C#.net in Asp.net
OK! first youll have to write the javascript code using HTML in the client side using the selectedIndex function! you could read more about javascript in google , second you you bind your data in server side like this
dropdown1.DataSource = datatable;
dropdown1.DataBind();
OR read these links
http://shawpnendu.blogspot.com/2009/08/javascript-how-to-get-selectedtext-from.html
http://shawpnendu.blogspot.com/2009/05/how-to-bind-or-populate-data-into.html
i hope it helps you or at least show you the right directions or hints of what you want :)
Try this one ...you can achieve it
<script type="text/javascript">
function func() {
var grd = document.getElementById("Panel1");
grd.setAttribute("style", "display:inline;");
}
</script>
<asp:TextBox ID="txtDDL" CssClass="txtClass" runat="server" ReadOnly="true" onclick="javascript:func();" />
<asp:Panel ID="Panel1" runat="server" Style="display: none;">
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinkButton ID="lbtnSelect" Text="Check All" runat="server" OnClick="Select" />
<asp:Button ID="btnSubmit" CssClass="btn" Text="Submit" runat="server" OnClick="Result" />
<asp:LinkButton ID="lbtnClear" Text="Clear All" runat="server" OnClick="Clear"></asp:LinkButton>
</asp:Panel>

Categories

Resources