How to invoke Html button control in repeater control - c#

Below is my code,can anyone please guide me in invoking the html button on repeater control.
Mark up:
<asp:Repeater ID="rptList" runat="server" onitemcommand="rptList_ItemCommand1">
<HeaderTemplate>
<table id="tbllist">
<tr>
<th>Qty</th>
<th width="100%" align="left">Item</th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><%# Eval("Quantity")%></td>
<td align="left"><%# Eval("ItemName")%></td>
<td align="center"><input id="Button1" runat="server" commandname="btnAdd" type="button" value="Addtocart" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
.CS file code:
protected void rptList_ItemCommand1(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if (e.CommandName == "btnAdd")
{
Response.Write("hello,I am a button.");
}
}

You have used input without runat="server"
Change
<input id="btnAddtocart" type="button" value="Addtocart" />
To
<input id="btnAddtocart" runat="server" type="button" value="Addtocart" CommandName="CommandName"/>
Use asp:button instead of input type="button"
<asp:Button ID="btnAddtocart" runat="server" CommandName="CommandName"/>

You have used input without runat="server", You need to add runat="server" to make a button server button control and perform postback
Change
<input id="btnAddtocart" type="button" value="Addtocart" />
To
<input id="btnAddtocart" runat="server" type="button" value="Addtocart" />
Use asp:button instead of input type="button" if you do not have any particular reason for using it.
<asp:Button ID="btnAddtocart" runat="server" Text="Addtocart" CommandName="Addtocart"/>

Related

Unusable elements inside the (list view ) asp.net

Why can't I use the (input) I put in my (list view) in the C # code field?
<asp:ListView ID="ListV" runat="server">
<ItemTemplate runat="server">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<tr class="text-center">
<td class="product-remove"></td>
<td class="image-prod">
<div class="">
<asp:Image ID="Image1" CssClass=" img" ImageUrl='<%# "../img/" + Eval("picture")
%> ' runat="server" />
</div>
</td>
<td class="product-name"><%# Eval("namebook") %> </td>
<td class="price"><%# Eval("Price") %> </td>
<td class="quantity"><%# Eval("titel") %></td>
<td class="col-2">
<asp:Button ID="delete" CssClass="btn btn-outline-danger" CommandArgument='<%#
Eval("id")%>' OnClick="delete_Click" runat="server" Text="حذف کالا" />
</td>
<td>
<input id="quantity2" runat="server" type="number" value="1" min="1" max="20" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
You want a server control there. Additionally, this control only exists as part of an item template, meaning there isn't just one control named quantity2... there could be many, depending on the data source. Therefore referencing it just by name is not good enough. And speaking of data sources, depending on how you handle this the databinding may not have taken place yet at the time the Page_Load code runs, meaning the input control doesn't exist at all.
So the real question is: what do you want to do with this input?
CodeBehind="~/ (Page Name) .aspx.cs"

Unable to check RadioButton inside DataList footer Template

I have kept two radio button inside a datalist footer template but I am not at all able to select it. Below is my footer template:
<FooterTemplate>
<tr>
<td align="right" colspan="7"><br />
</td>
</tr>
<tr>
<td align="left" valign="middle" colspan="7">
<div style="background-color:#FCEE21; height:47px ;">
<div style=" position:relative; top:15px; left:20px;">
<asp:CheckBox ID="cbAgree" Checked="true" Text="I agree on all terms & conditions" runat="server" />
</div>
<div style=" position:relative; top:-1px; left:410px;">
<asp:Label ID="Label2" runat="server" Text="Total: (INR)"></asp:Label>
</div>
<div style=" position:relative; top:-22px; left:550px;">
<asp:Label ID="lbltotal" runat="server" Text="000000" Font-Size="14pt"></asp:Label>
</div>
</div>
</td>
</tr>
<tr>
<td align="right" colspan="7">
<div style="background-image: url(images/footerBg.png); height:47px ;">
<div style=" position:relative; top:10px; left:-172px;">
<asp:Label ID="Label9" runat="server" Text="Select an option: "></asp:Label>
<asp:RadioButton ID="rb1" Text="Colect" AutoPostBack="true" runat="server" GroupName="rboption"/>
<asp:RadioButton ID="rb2" Text="Deliver" AutoPostBack="true" runat="server" GroupName="rboption"/>
</div>
<div style=" position:relative; top:-18px;">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" BackColor="#F8CD20" CssClass="anchor"
BorderColor="#F8CD20" BorderStyle="None" OnClick=" Submit_Clicked" Font-Bold="True" Font-Size="14pt"
Height="38px" Width="105px" />
</div>
</div>
</td>
</tr>
</FooterTemplate>
I have not done any databinding also. Please help me out, if I missed out something.
Your problem is about your css styling. If you remove inline css styles from your divs you can click them.
Those divs are displaying on top of each other without proper styles and radiobuttons stay under another div, prevents you clicking on them.
Or if you want to keep the styles and still click them you can use "z-index" attribute with adding z-index:1;, so you bring the div with radiobuttons, above the other div, making them clickable.
....
<div style=" position:relative; top:10px; left:-172px; z-index:1;">
<asp:Label ID="Label9" runat="server" Text="Select an option: "></asp:Label>
<asp:RadioButton ID="rb1" Text="Colect" AutoPostBack="true" runat="server" GroupName="rboption"/>
<asp:RadioButton ID="rb2" Text="Deliver" AutoPostBack="true" runat="server" GroupName="rboption"/>
</div>
....

Required Validator and compare validators do not work!

I am trying to see the validation part working. I have few required field validators and compare field validators, etc.
<div>
<asp:RequiredFieldValidator ID="rfvCompany" ValidationGroup="groupProfile"
ControlToValidate="txtCompany" runat="server"
ErrorMessage="- Company Name Required" Display="Dynamic" />
</div>
<div>
<asp:RequiredFieldValidator ID="rfvAddress" ValidationGroup="groupProfile"
ControlToValidate="txtAddress" runat="server"
ErrorMessage="- Address 1 Required" Display="Dynamic" />
</div>
This is my save button, validation has to happen when this button is clicked.
<tr>
<td align="center">
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/images/green-save.gif"
OnClick="btnSave_Click" TabIndex="22" ValidationGroup="groupProfile" />
</td>
</tr>
The popup that comes when the save button is clicked is this..
<tr>
<td colspan="2" align="left" style="padding-left: 75px; padding-top: 10px;">
Do you wish to update the Location Information as well.
</td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:Button ID="btnYesMerchant" Text ="Yes" runat="server" class="popupButton"
causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;"
onclick="btnYessave_Click"/>
<asp:Button ID = "btnNoMerchant" Text ="No" runat ="server" class="popupButton"
causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;"
onclick="btnNosave_Click"/>
<asp:Button Id="btnCancel" Text ="Cancel" runat="server" class="popupButton" />
</td>
</tr>
Where am i doing wrong? i am in a serious mess, i guess :(
Ensure you have included the asp:ScriptManager on your page. In addition to that check for javascript errors and (if any) add them to your question.
Here i have assigned the popup to the button, so the popup is being called when the button is clicked. Now i have changed the flow. For the button, i have written a javascript function that will validate and calls the popup to show if validation is passed. This worked. Thank you all..

Yes No Cancel (3 buttons) for AJAX Modalpopupextender

Does Ajax ModalPopupExtender has an option to create 3 buttons? Yes, No and Cancel? for ASP.NET WebForms, C#
I could not come up with a solution. I can just find OKControlID and CancelControlID.
<table id="pnlPopupMerchantUpdate" runat="server" style="display:none">
<tr>
<td>
<asp:Panel runat="server" CssClass="modalPopup">
<table width="350" height="80" class="warningPopup">
<tr>
<td>
<!-- <img src="images/warning_blue.gif" alt="Warning" /> -->
</td>
<td colspan="2" align="left" style="padding-left: 75px; padding-top: 10px;">
Do you wish to update the Location Information as well.
</td>
</tr>
<tr>
<td align="center" colspan="4">
<input id="btnYesMerchant" type="button" value="Yes" class="popupButton" causesvalidation="true" onclick="btnYessave_Click"/>
<input id="btnNoMerchant" type="button" value="No" class="popupButton" causesvalidation="true" onclick="btnNosave_Click" />
<input id="btnCancel" type="button" value="Cancel" class="popupButton"/>
</tr>
</table>
</asp:Panel>
</td>
</tr>
Here, I do want to call different functions for Yes and No.
Instead of OKontrolID, you can use extender's client side API to hide it on button's on-click event. See this article that shows multiple cancel button - your scenario is similar, you just need to return true from your button's onclick event handler.
<ajaxToolkit:ModalPopupExtender runat="server" BehaviorID="myPopup" ...
and
<input id="btnYesMerchant" type="button" onclick="$find('myPopup').hide(); return true;" ...
Thought i found it to be so tough, it is simple to handle this problem. We just got to add return true for each asp button.
ANd remove the OKCOntrolID from ModalPopupExtender. It solved my problem. Thanks all who gave it a try reading.

Button not firing onClick below Panel controls in IE

I have a button which is not working when placed below two panels. If I move it above the panels, it works.
It works either way in Firefox. It does not work in IE 8
The button runs this code
protected void Button2_Click(object sender, EventArgs e)
{
panelForm.Enabled = true; //input panel
panelOutput.Visible = false; //output panel
Button1.Visible = true; //input panel button
}
I have some workarounds, but was hoping to find the cause of the issue.
edit: here is the markup of the second panel and button. I've tried moving the button outside of the panel and get the same result.
<asp:Content ID="MainContent" Runat="Server" ContentPlaceHolderID="MainContentPlaceHolder">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="domainUserID" runat="server" Visible="false"></asp:TextBox>
<!-- gray bar and title -->
<table style="width:100%; border-style:none;">
<tr>
<td class="com_headline">
SQL Emergency Request [Home]
</td>
</tr>
<tr class="com_app_instructions">
<td>
<p>Words here</p>
</td>
</tr>
</table>
<!-- end title and gray bar -->
<asp:Panel ID="panelForm" runat="server" Visible="True" CssClass="myform">
<form method="post" action="Default.aspx" id="form">
<h1>Request Form</h1>
<p>Complete this form to be issued a login</p>
<table cellpadding="5px">
<tr>
<td>
IR Number
<br />
<span class="small">Obtain your IR number from
SMART</span>
</td>
<td>
<asp:TextBox ID="txtIR" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtIR" CssClass="errorMsg"
ErrorMessage="Please Enter Your IR Number">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Server
<br />
<span class="small">MSSQL5 is supported for now</span>
</td>
<td>
<asp:DropDownList ID="ddServer" runat="server" AutoPostBack="True"
Enabled="False" onselectedindexchanged="ddServer_SelectedIndexChanged">
<asp:ListItem>DEVMSSQL05</asp:ListItem>
<asp:ListItem Selected="True">MSSQL05</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="ddServer" CssClass="errorMsg"
ErrorMessage="Please Choose A Server">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Database
<br />
<span class="small">You have the role of 'Analyst' in these databases</span>
</td>
<td>
<asp:DropDownList ID="ddDatabase" runat="server" AppendDataBoundItems="true"
AutoPostBack="false" DataSourceID="DatabaseDropDownObjectDataSource"
DataTextField="DatabaseName" DataValueField="DatabaseName" Width="150">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" CssClass="com_btn_flat"
onclick="Button1_Click" Text="Submit" />
</td>
<td>
<div id="loader">loading...</div>
</td>
</tr>
</table>
</form>
</asp:Panel>
<asp:Panel ID="PanelError" runat="server" Visible="false" CssClass="errorPanel">
<h1><asp:Label ID="txtErrorMsg" runat="server">error text</asp:Label></h1>
</asp:Panel>
<br />
<asp:Panel ID="panelOutput" runat="server" Visible="false" CssClass="panelOutput">
<h1>
<asp:Literal ID="Title" runat="server" Text=""/>
</h1>
<p>
<asp:Literal ID="Warning" runat="server" Text=""/>
</p>
<p>
<asp:Literal ID="LoginLifeHours" runat="server" Text=""/>
</p>
<p>
<span class="important">
<asp:Literal ID="Login" runat="server" Text="" />
</span>
</p>
<p>
<span class="important">
<asp:Literal ID="PWD" runat="server" Text="" />
</span>
</p>
<br />
<p>
<asp:Button ID="Button2" runat="server" Text="Request Another Login"
onclick="Button2_Click" CssClass="com_btn_flat" />
</p>
</asp:Panel>
This is the button that is not responding in IE
<p>
<asp:Button ID="Button2" runat="server" Text="Request Another Login"
onclick="Button2_Click" CssClass="com_btn_flat" />
</p>
The problem is that you are using a <form> tag within your content page. The master page already includes a <form> tag and IE appears to be balking at the form within a form. When I removed the <form> tag from your aspx, the button handler ran under IE8.
The sample code is Button2_Click, but your button markup outside of the panels has test_button_Click as the event handler? There are three buttons, so which one are you asking about, I assume the last?
I noticed that you have a tag inside your first panel (panelForm). Also, I don't see a a tag with a runat="server" attribute (although, it could be in a master page). And it doesn't look like your button is inside a form (unless the master page has a form).
The problem is that you can only have one form on a page in Asp.NET WebForms. If you don't have any <form runat="server"> tags on your page, then you buttons will not fire any events on the code-behind.

Categories

Resources