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}
Related
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;
}
I am trying to get the required field validation to fire only for the specific row when the add button is clicked. But right now, when I click any add button, it fires the required field validation for all nested grid rows. The code for the nested gridview and the image of the gridviews is the following. I also tried to use validation groups, attempting to get unique validation groups for each nested gridview row, but couldn't get it to work.
<asp:GridView ID="GrdXML" runat="server" AllowSorting="false"
AutoGenerateColumns="False" CellPadding="0"
ForeColor="#333333" GridLines="Horizontal" onrowdeleting="GrdXML_RowDeleting" OnRowDataBound="OnRowDataBound"
ShowFooter="false" CssClass="xmlgrid" OnSorting="GrdXML_Sorting" DataKeyNames="ContractRecordID">
<Columns>
<asp:TemplateField HeaderText="Contract Start Date" SortExpression="ContractStartDate">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ContractStartDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Contract End Date" SortExpression="ContractEndDate">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ContractEndDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server"
CommandArgument='<%# Eval("ContractRecordID") %>' CommandName="Delete"
onclick="BtnDelete_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal runat="server" ID="lit1" Text="<tr id='trGrid'><td colspan='100%'>" />
<asp:GridView ID="projectCodeGridView" runat="server" AllowSorting="false" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="ProjectCodeGridView_RowCommand" OnRowCreated="ProjectCodeGridView_RowCreated" OnRowDataBound="ProjectCodeGridView_RowDataBound" OnRowDeleting="ProjectCodeGridView_RowDeleting" DataKeyNames="ContractProjectCodeID" BorderStyle="None" BorderWidth="0">
<Columns>
<asp:TemplateField HeaderText="Project Codes" SortExpression="ContractProjectCode" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ContractProjectCode") %>' CssClass="projectcodelabel"></asp:Label>
</ItemTemplate>
<ItemStyle Width="100%" />
<FooterTemplate>
<asp:TextBox ID="projectCodeTextBox" runat="server" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="projectCodeRequiredFieldValidator" runat="server"
ControlToValidate="projectCodeTextBox" ErrorMessage="Project Code Required" ForeColor="Red" Display="Static" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete1" runat="server"
CommandArgument='<%# Eval("ContractProjectCodeID") %>' CommandName="Delete"
onclick="BtnDelete1_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete1_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete1">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" Width="60px" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' CommandName="Add" />
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#F7F6F3" Font-Bold="True" />
</asp:GridView>
<asp:Literal runat="server" ID="lit2" Text="</td></tr>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="Black" CssClass="padding" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" CssClass="padding" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
For the validation control of each row you are adding the ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'. As you are saying, that the validation triggers for the wrong row, it could be that ContractProjectCodeID may not be unique for each row. I think what you can do is add row index to the validation group
ValidationGroup='<%# "PC" + Container.DataItemIndex + Eval("ContractProjectCodeID")
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")%
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>
I'm trying to get the value from detailsview to formview. I want to set the Book ID/ISBN in formview insert textbox to Book ID/ISBN value from detailsview.
Here's a sample of my code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="bookid" DataSourceID="detailsDataSource"
ForeColor="#333333" GridLines="None" Height="50px" Width="">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<FooterTemplate>
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="reservationid" DataSourceID="reserveDataSource"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Reserved by"
SortExpression="EmployeeID" />
<asp:BoundField DataField="reservedate" HeaderText="Reserved date"
SortExpression="reservedate" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="reservationid"
DataSourceID="reserveDataSource">
<EditItemTemplate>
reservationid:
<asp:Label ID="reservationidLabel1" runat="server"
Text='<%# Eval("reservationid") %>' />
<br />
bookid:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<br />
EmployeeID:
<asp:TextBox ID="EmployeeIDTextBox" runat="server"
Text='<%# Bind("EmployeeID") %>' />
<br />
reservedate:
<asp:TextBox ID="reservedateTextBox" runat="server"
Text='<%# Bind("reservedate") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Book ID/ISBN:
<asp:TextBox ID="bookidTextBox" runat="server"
Text='<%# Bind("bookid") %>'/>
<br />
Employee ID:
<asp:TextBox ID="EmployeeIDTextBox0" runat="server"
Text='<%# Bind("EmployeeID") %>' />
<br />
Reserve date:
<asp:TextBox ID="reservedateTextBox0" runat="server"
Text='<%# Bind("reservedate") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Reserve" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
So what I'm trying to do is when a user click the Insert/Reserve book linkbutton the Book ID/ISBN is already set to the Book ID/ISBN in formview insert mode as default.
Any help would be much appreciated ;)
Thanks in advance.
Can you try and check the DetailsView1.SelectedValue
<asp:TextBox ID="bookidTextBox" runat="server"
Text='<%# DetailsView1.SelectedValue %>'/>
Edit:
Now if you want to bind that value to your Insert function of your DetailsView, it will not behave like the other control, since you are using the Bind method for the other control and it provides two way binding.
Now you need to pass that value to inserting evet of DetailsView like as we are assigning values using DetailsView1.SelectedValue but not binding the value.
I hope you understand this theory.
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["bookid"] = ((TextBox)DetailsView1.FindControl("bookidTextBox")).Text;
}