How can i put only one button for ChceckBox?
here is what i get:
enter image here
As you can see i have 10 button for each checkbox value but i need group them by "week" "Day" and in this case put only one for each of them. That "day weak" label come from sql and is dynamic value.
Here is code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PS_TestConnectionString %>" SelectCommand="SELECT * FROM [Prize] ">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Prize") %>' OnCheckedChanged="CheckBox1_CheckedChanged1" AutoPostBack="True" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /><br /> <asp:Button ID="Button1" runat="server" Text="Receive the prize" Visible="True" CommandName="foo" /></ItemTemplate>
I need this witch CheckBox in itemtemplate becouse i have to put image in
I solved this problem by adding a new GridView to ItemTemplate
Related
working on an ASP Gridview querying a SQL server base.
<asp:Content ID="i_cttContenu" runat="server" ContentPlaceHolderID="i_cphContenu">
<asp:SqlDataSource ID="i_sdsGvOption" runat="server" ConnectionString="<%$ ConnectionStrings:... %>"
SelectCommand=" SELECT * FROM MyTable " SelectCommandType="Text"
UpdateCommand="UPDATE MyTable SET [name] = #name, prenom = #prenom, isAlive = #isAlive WHERE idWsgProgramOption = #idWsgProgramOption" UpdateCommandType="Text"
</asp:SqlDataSource>
<asp:UpdatePanel ID="i_up" runat="server">
<ContentTemplate>
<asp:GridView ID="i_gvOption" runat="server" AutoGenerateColumns="False" DataKeyNames="idWsgProgramOption"
DataSourceID="i_sdsGvOption" EnableModelValidation="True">
<Columns>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/....gif"
CancelText="Annuler" EditImageUrl="~/....gif"
EditText="Update" HeaderText="M" UpdateImageUrl="~/....gif"
UpdateText="Save">
</asp:CommandField>
<asp:TemplateField HeaderText="Nom" SortExpression="name">
<ItemTemplate>
<asp:HyperLink ID="i_hlOption" runat="server" NavigateUrl='<%# Eval("idWsgProgramOption", "~/myURL") %>'
Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<table >
<tr>
<td >
<asp:TextBox ID="i_tbNom" runat="server" Text='<%# Bind("name") %>' />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="prenom" SortExpression="prenom">
<ItemTemplate>
<asp:HyperLink ID="i_hlprenom" runat="server" NavigateUrl='<%# Eval("prenom", "~/myURL") %>'
Text='<%# Eval("prenom") %>' />
</ItemTemplate>
<EditItemTemplate>
<table >
<tr>
<td >
<asp:TextBox ID="i_tbprenom" runat="server" Text='<%# Bind("prenom") %>' />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Obligatoire" >
<ItemTemplate>
<asp:CheckBox ID="CB_id1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CB_id2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The update() method is not invoked when I click the update button (first column). If I add an onUpdating event into the datasource, the corresponding method is never invoked.
The code causing the issue is definitely the checkbox.
If I remove from the datasource update query:
, isAlive = #isAlive
and only set:
UPDATE MyTable SET [name] = #name, prenom = #prenom, isAlive = #isAlive WHERE idWsgProgramOption = #idWsgProgramOption;
Then it updates fine (except the isAlive field of course).
I am 100% sure that the isAlive field exists into the base (bit type).
So it looks that my problem comes from this bloc:
<asp:TemplateField HeaderText="Obligatoire" >
<ItemTemplate>
<asp:CheckBox ID="CB_id1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CB_id2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
Is there anything obvious that I missed??
Also, this is the simplified code, but if I set the "checked" property to the checkbox and correctly binds, the rows checkbox is correctly field. So the issue does not affect the select but only the update.
Try a button column instead so you can specify the specific command names.
<asp:ButtonField ButtonType="Link" Text="Update" CommandName="Update" />
<asp:ButtonField ButtonType="Link" Text="Delete" CommandName="Delete" />
Take action based on e.CommandName.
I've searched in google and bing and seen all posts and even in stackoverflows search but couldn't find the solution.
To be sure about the error I've created a new project it has 1 page which has:
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label><br/>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource" SelectCommand="Select * FROM Users" ConnectionString="<%$ConnectionStrings:DefaultConnection %>"></asp:SqlDataSource>
Output:
Because I've 2 users with name A and B.
Now the problem is that events OnSelectedIndexChanged and OnSelectedIndexChanging are not firing when I click on A or B. How to get the selected index where user is clicking?
Complete Project if anyone wants to look, actually there is no need to: https://app.box.com/s/bhs682q6fqai784kao0zv1tcxgxecr4s
ListView.SelectedIndexChanging Event occurs when an item's Select button is clicked, but before the ListView control handles the select operation.
(A Select button is a button control whose CommandName property is set to "Select".)
Change your code to
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging" AutoPostBack="true">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label>
<asp:LinkButton runat="server" ID="ButtonSelect" CommandName="Select" Text="SelectButton"/><br/>
</ItemTemplate>
</asp:ListView>
I have a gridview which contains images (populated dynamically from database) and dropdownlist which contains two values. First column contains checkbox. I want to insert selected checkbox's images and dropdown values to a new table on button click. What may be the suitable way?
Here is the grid view:
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" AllowPaging="true"
EmptyDataText="No images found" OnPageIndexChanging="gvDetails_PageIndexChanging" PageSize="5">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckUncheckAll"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID ="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgPreview" ImageUrl='<%#
"ImageHandler.ashx?imgID="+ Eval("ID") %>' runat="server"
Height="80px" Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dropdown" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstatus" runat="server" OnSelectedIndexChanged="dpdListEstatus_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
Loop with gridview rows
Find for the checkbox control
Check if its Checked property is true
If yes, call insert statement
Set the values you want to get from image and dropdownlist. Of course you need to use findcontrol on it too.
Dim cbSelect As CheckBox, imgToInsert As Image, ddlStatus As DropDownList
For Each r As GridViewRow In gvDetails.Rows
cbSelect = r.Cells(0).FindControl("CheckBox2")
If cbSelect.Checked Then
imgToInsert = r.Cells(1).FindControl("imgPreview")
ddlStatus = r.Cells(2).FindControl("dpdListEstatus")
'Insert statement goes here...
End If
Next r
I'm trying to get the original object back from a listview, when I click on the button.
I thought it was in e.Item.DataItem but that always seems to be null.
<form runat="server">
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="tbCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:TextBox ID="tbEmailAdress" runat="server" Text='<%# Eval("EmailAddres") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<asp:Button runat="server" Text="Create or Update Account" CommandArgument='<%# Container.DataItem.ToString() %>' />
<br />
</ItemTemplate>
</asp:ListView>
</form>
private void ListList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
You need to keep the original datasource in session and get the real values while clicking a button.
Gridview has many columns and a Delete button as well. Delete button is a control as TemplateField
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" Text='<%# Eval("disabled").ToString()=="False" ? "Disabled" : "Enabled" %>'
OnClientClick="return confirm('Are you sure you want to take this action?');"
runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Now associated SQLDataSource's Delete command's stored procedure is expecting two parameters. One is going from DataKeyNames (RowID), other one i wanna pass is the Text of btnDelete (True or False).
How can i achieve it?
I would recommend doing it in the code behind. On btnDelete click i would iterate through every row in your gridview and check all the datakeynames. Once i found the one you want to delete you will need to send that back to you db. You can use either an orm like linq, ado.net, or a straight sqlcmd.
Please See this Code. I implement and works fine
<asp:TemplateField HeaderText="Add" HeaderStyle-CssClass="grid_Title">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("TrackID")%>' />
<asp:SqlDataSource ID="SqlDataSource_Projects" runat="server" ConnectionString="<%$ ConnectionStrings:SentricMusicFunctionalityConnectionString2 %>"
SelectCommand="select* from MassTraxCatProjects where Fk_AgencyId=#CatalogAgencyId and fk_trackid=#TrackID) ">
<SelectParameters>
<asp:SessionParameter DbType="Int32" DefaultValue="CatalogAgencyId" Name="CatalogAgencyId"
SessionField="CatalogAgencyId" />
<asp:ControlParameter DefaultValue="TrackID" Name="TrackID" ControlID="**HiddenField1**" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddl_ProjectType" runat="server" DataSourceID="SqlDataSource_Projects"
Width="100px" DataTextField="ProjectName" DataValueField="ProjectId">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>