like in title + how to handle button click which button is in GridView Footer also?
file .aspx seems like this
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="id" EnableModelValidation="True"
ForeColor="#333333" GridLines="None"
onrowcancelingedit="GridView1_RowCancelingEdit1"
onrowediting="GridView1_RowEditing1"
onrowupdating="GridView1_RowUpdating1" AllowPaging="True"
onrowdeleting="GridView1_RowDeleting"
onpageindexchanging="GridView1_PageIndexChanging" Height="322px"
ShowFooter="True" onselectedindexchanged="GridView1_SelectedIndexChanged" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DriveLic" HeaderText="DriveLicense" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtDriveLicense" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="country" HeaderText="Country" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtWoj" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="ButtonOK" Text="Confirm" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
Inside your GridView_RowCommand Event you can access the footer control by GridView1.FooterRow.FindControl method.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase))
{
TextBox txtSomeNewValue = ((TextBox)GridView1.FooterRow.FindControl("txtSomeNewValue"));
string theTextValue = txtSomeNewValue.Text;
}
}
Update: wrapped the code in an if block that checks if the commandname is what you were expecting. This event is also used for delete, edit, etc, so you might end up running the code for an event you didnt intend if you dont wrap it in this.
Related
After checking a condition I want to hide or view an image button inside a gridView.
I tried to like this. but it is not working.
<asp:GridView ID="GridView1" runat="server" Width="100%" CssClass="grid" AutoGenerateColumns="False"
EnableModelValidation="True" DataKeyNames="T_ID" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="TEMPL_TITLE" HeaderText="Title">
<HeaderStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="TEMPL_DESC" HeaderText="Description">
<HeaderStyle Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="Category" DataField="CAT_NAME" />
<asp:BoundField HeaderText="Workflow Type" DataField="WFTYPE_DESCRIPTION" />
<asp:BoundField HeaderText="Owner" DataField="EMP_CALLING_NAME" />
<%# if(Eval("EXP_PENDING_APPR").ToString() == "0") { %>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ViewImageButton1" runat="server" CommandArgument='<%# Eval("TEMPL_ID") %>'
ImageUrl="Images/jdAttView.gif" OnCommand="btnViewTemplate_Click" ToolTip="View and download template" />
</ItemTemplate>
</asp:TemplateField>
<% } %>
</Columns>
<EmptyDataTemplate>
No templates have been assigned to you security capabilites, Please contact your
local administrator for more information.
</EmptyDataTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
The error with this code
The error with this code
You can apply condition on server control itself.
<asp:ImageButton Visible='<%# Eval("EXP_PENDING_APPR").ToString().Equals("0") %>' ID="ViewImageButton1" runat="server" CommandArgument='<%# Eval("TEMPL_ID") %>' ImageUrl="Images/jdAttView.gif" OnCommand="btnViewTemplate_Click" ToolTip="View and download template" />
In GridView how can I check the cbSelect when txtBox text changed in the same row using JQuery or Javascript.
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtBoxt" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
you can use java script onkeyup event and set the checkbox value as checked. You can use below code.
<script type="text/javascript">
function setCheckBox(selectedRow) {
var row = selectedRow.parentNode.parentNode;
row.cells[0].children[0].checked = true;
}
</script>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtBoxt" runat="server" onkeyup="setCheckBox(this); "/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
you can change cell index, event and function name according to your requirement.
Currently I have 3 checkboxes in gridview which are Approval,Access and Edit. What I want to achieve is when I checked the Edit column, the Access column will be checked automatically too. Is there any way to solve this? I have looked through a lot internet source but still not able to do it. Please help me..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowSorting="True" BorderStyle="Solid" Font-Names="Tahoma" Font-Size="11pt" HorizontalAlign="Center" Width="65%">
<Columns>
<asp:BoundField DataField="Module" HeaderText="Module" SortExpression="Module" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Approval">
<ItemTemplate>
<asp:CheckBox ID="CBApproval" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Access">
<ItemTemplate>
<asp:CheckBox ID="CBAccess" runat="server" AutoPostBack="False" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:CheckBox ID="CBEdit" runat="server" AutoPostBack="True"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#FFCC00" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" />
</asp:GridView>
Sinc edit checkbox postback you may set the value of accessed checkbox from codebehind.. Add new event to edit checkbox on click and set new value for accessed checkbox.checked
I have the following GridView:
<asp:GridView ID="gvDispMsg" ClientIDMode="Static" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" EmptyDataText="There is no data to display" runat="server" AllowPaging="false" AllowSorting="true">
<Columns>
<asp:BoundField HeaderText="Delete Message" HeaderStyle-Width="12%" />
<asp:BoundField HeaderText="ID" HeaderStyle-Width="12%" DataField="ID" />
<asp:BoundField HeaderText="Date" HeaderStyle-Width="13%" DataField="Created" />
<asp:BoundField HeaderText="Message" HeaderStyle-Width="45%" DataField="Message" />
<asp:BoundField HeaderText="Active" HeaderStyle-Width="10%" DataField="Active" />
<asp:BoundField HeaderText="Created By" HeaderStyle-Width="20%" DataField="CreatedBy" />
</Columns>
</asp:GridView>
It is populated from a SQL table which is executed from code-behind. The only column which is not populated is the Delete Message column.
How can I add a button for each row in the Delete Message column which takes the argument for the ID datafield?
Add
<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"
and handle the NewEvent in CodeBehind
or
add a <asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/> instead of a BoundField
or
add
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</FooterTemplate>
</asp:TemplateField>
where you have most abilities to configure this column in every state
I have a couple of gridviews in my website. Here's the code:
changeSlides.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="PlaceImages/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="PicPath" HeaderText="Filename" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
EditBlogPost.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateEditButton="True"
DataSourceID="AccessDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
AlternatingRowStyle-BackColor="Gray"
AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"
RowStyle-VerticalAlign="Top"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="Gray" CssClass="editGridFormat"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="BlogTitle" HeaderText="BlogTitle"
SortExpression="BlogTitle" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image"
DataImageUrlFormatString="~/PlaceImages/{0}" ControlStyle-CssClass="editPhotoGridFormat"
AlternateText="Either a wrong file type or a misspelled file name has occurred"
NullDisplayText="No picture on file" >
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="Caption"
HeaderText="Caption for Picture on Homepage" />
<asp:TemplateField headertext="Text for the homepage">
<EditItemTemplate>
<asp:TextBox id="PicTextBox" runat="server" text='<%# Bind("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("PicText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TravelDate" HeaderText="Date of Travel"
SortExpression="TravelDate" DataFormatString="{0:MMMMMMMMM, dd, yyyy }" HtmlEncode="false" />
<asp:TemplateField headertext="Top of the Blog Post">
<EditItemTemplate>
<asp:TextBox id="BeginTextBox" runat="server" text='<%# Bind("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("BeginText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField headertext="Text at the End of the Post">
<EditItemTemplate>
<asp:TextBox id="EndTextBox" runat="server" text='<%# Bind("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("EndText")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Top" CssClass="editGridFormat"></RowStyle>
</asp:GridView>
EditPeoplePhotos.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataSourceID="AccessDataSource1" Width="493px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="photos/PeoplePhotos/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
</asp:ImageField>
</Columns>
</asp:GridView>
My update statements work, but my delete statements don't work in two of the three gridviews. My corresponding delete statements are all virtually the same: "DELETE FROM (Table Name) WHERE ID=?" but I get different error codes, which are as follows:
For changeSlides.aspx, it says "Entry with same key already exists"
For EditBlogPosts.aspx, it work fine. It deletes the blog.
For EditPeoplePhotos.aspx, it says "No value given for one or more required parameters"
Each of those pages use a separate, unrelated table in my database, and each query is only referring to one table, so there are no foreign keys used.
What is going on?