umbraco user control form create it's own action - c#

I need help with Umbraco user control.
I create a user control to get input from user.
ascx file
<form id="formRegister" runat="server">
<table>
<tr>
<td>
<asp:TextBox ID="txtUserName" type="text" placeHolder="User Name" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" ClientIDMode="Static" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
the ascx.cs file
protected void Page_Load(object sender, EventArgs e)
{
var b = 0;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
var a = 0;
}
but somehow when it's loaded i get this in firebug
<form id="formRegister" action="/register/" method="post">
So the form create it's own action and do that action when I click the submit button instead of the btnSubmit_click event.
What should I do?

User controls won't fire their on click events when loaded as a macro in an Umbraco MVC site. Create a partial view and surface controller instead.

Related

FindControl() always returns null from DataList Textbox

I have 8 items in a SQL table with a Description, ItemNumber, and ImagePath.
protected void Page_Load(object sender, EventArgs e)
{
//fill the datalist for the Signs Table
string tCallFrom = "Program." + System.Reflection.MethodBase.GetCurrentMethod().Name;
string tQry = #"SELECT * FROM [js_Signs]";
DataTable tblSigns = objCommMethods.fReturnTableFromQry(clsDBSelect.enumDBSelect.ProjectMaster, tQry, tCallFrom);
SignsList.DataSource = tblSigns;
SignsList.DataBind();
}
I am using a DataList to show all of the items along with a textbox that will allow a user to input the number of items they want:
<asp:DataList ID="SignsList" runat="server" RepeatColumns="4" CellPadding="2" Width="90%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<table>
<tr>
<th>
<%#DataBinder.Eval(Container.DataItem, "Description") %>
</th>
</tr>
<tr>
<th>
<%#DataBinder.Eval(Container.DataItem, "ItemNumber") %>
</th>
</tr>
<tr>
<td>
<asp:Image ID="SignImage" runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "ImagePath") %>' />
</td>
</tr>
<tr style="text-align: center;">
<td>
<asp:TextBox ID="txtSignQuantity" runat="server" CssClass="txtBox" BackColor="White" Enabled="true"
type="number" min="0" ToolTip="Please choose quantity of signs needed."/>
</td>
</tr>
<tr>
<td>
<p> </p>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<div class="btnGroup">
<div class="btnDiv">
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btnSave" Width="90px" Visible="true" OnClick="btnSave_Click"/>
</div>
<div class="btnDiv">
<asp:Button ID="btnCancel" runat="server" Text="Clear" CssClass="btnCancel" Width="90px" Visible="true" OnClick="btnCancel_Click"/>
</div>
</div>
After the saved button is clicked in C# I want to find out the value that a user entered but it is always returning null:
protected void btnSave_Click(object sender, EventArgs e)
{
TextBox txtSignQuantity;
string tempSignQuantity;
try
{
foreach (DataListItem item in SignsList.Items)
{
txtSignQuantity = item.FindControl("txtSignQuantity") as TextBox;
tempSignQuantity = txtSignQuantity.Text;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I also checked and the Count of SignsList.Items was 8, so I know that it is retrieving the correct information. Sorry, I've never used a Data List before so I'm not really sure how to go about this...
The short answer is that data binding on postback is causing the problem. Check the Page.IsPostBack property before doing data binding:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//fill the datalist for the Signs Table
string tCallFrom = "Program." + System.Reflection.MethodBase.GetCurrentMethod().Name;
string tQry = #"SELECT * FROM [js_Signs]";
DataTable tblSigns = objCommMethods.fReturnTableFromQry(clsDBSelect.enumDBSelect.ProjectMaster, tQry, tCallFrom);
SignsList.DataSource = tblSigns;
SignsList.DataBind();
}
}
ASP.NET WebForms attempts to abstract away the fact that the control instances that were used to render the page are not the same instances it has when handling postback events. Data binding compounds the abstraction because it has to create controls in response to the DataBind call, and then on postback, recreate them based on the ViewState it saved.
Initializing controls from ViewState happens on the Init event, so when the Load event is fired later in the page lifecycle and you call DataBind on the control, everything it restored gets wiped out and recreated.
As for why you were getting null, it may have been that the controls were wiped out but not recreated; it may have had to do with other event handlers that didn't get rewired after the second data binding.

Text box getting cleared because of update panel

I've have one table, in that table within one I've one and within that , I've update panel whose update mode is set to conditional. Within this update panel I've another table. The table contains 3 text boxes as: old password, new password and confirm password. On the textChanged event of the old password I am checking the user entered value with the value in db. But when the function completes its execution all the 3 text boxes looses its values regardless of whether I update the update panel or not. I don't know why it clears text boxes. I want to prevent text boxes from getting cleared. I tried to get the text box text in string variable and again assign it to text boxes (both in text box text changed event and in page load event under isPostBack condition) but its too, not working.
asp code:
.
.
.
<tr>
<td colspan="3">
<div>
<asp:UpdatePanel ID="updPnlChngPwd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table style="width:100%">
<tr>
<td>
Old Password
</td>
<td>:</td>
<td>
<asp:TextBox ID="txtOldPwd" runat="server" Height="21px" MaxLength="50" TextMode="Password" Width="60%" ontextchanged="txtOldPwd_TextChanged"
AutoPostBack="True"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<asp:Label ID="lblWrongOldPwd" runat="server" Text="Wrong Old Password" ForeColor="Red" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>:</td>
<td></td>
</tr>
<tr>
<td></td>
<td>:</td>
<td>
<asp:TextBox ID="txtSuppRePwd" runat="server" Height="21px" MaxLength="50" TextMode="Password" Width="60%"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnUpdPwd" runat="server" Text="Change Password" onclick="btnUpdPwd_Click"/></td>
<td>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<td>
</tr>
.
.
.
C# code for tetxt box textChanged event:
protected void txtOldPwd_TextChanged(object sender, EventArgs e)
{
DataTable dtOldPwd = Obj.DBAccess("select Pwd from Customer where Cust_Id = " + Convert.ToInt32(Session["SuppID"]) + " and Supp_Pwd = '" + txtOldPwd.Text + "'");
if (dtOldPwd.Rows.Count == 1)
{
lblWrongOldPwd.Visible = false;
}
else
{
lblWrongOldPwd.Visible = true;
updPnlChngPwd.Update();
}
}
Now I am not able to understand what exactly wrong I am doing, does having update panel inside the table causing problem?
<td>
<asp:TextBox ID="txtSuppRePwd" runat="server" Height="21px" MaxLength="50" TextMode="Password" Width="60%"></asp:TextBox>
</td>
You have TextMode set to password which will not save your textbox value .
However you will get your textbox value as string on textchange event
protected void txtbx_TextChanged(object sender, EventArgs e)
{
string txtValue = txtbx.Text;
ViewState["xyz"]= txtValue;
}
and you have to save this value in ViewState to use it for btnClick event .
OR
You can also set textbox attribute at Page_Load event which is a very bad practice to do like this
protected void Page_Load(object sender, EventArgs e)
{
txtbx.Attributes.Add("value", txtbx.Text);
TextBox with TextMode="Password" will be cleared after postback or partial postback. This is the default behavior of the password textbox so submit all the data at a time and do the validation in your code.
Alternatively, You can store password in in viewstate or session and restore after postback.

return value to parent page from modal pop up extender

<asp:Button ID="btnCheckPatientID" CssClass="cssbutton" runat="server" Text="Check"
OnClick="btnCheckPatientID_Click" />
<asp:ModalPopupExtender ID="btnCheckPatientID_ModalPopupExtender" runat="server"
PopupControlID="panelCheckPatient" TargetControlID="hiddenTargetControlForModalPopup"
BackgroundCssClass="modalbackground" CancelControlID="btnClose">
</asp:ModalPopupExtender>
<asp:Button runat="server" ID="hiddenTargetControlForModalPopup" Style="display: none" />
<div id="panelCheckPatient" class="modalpopup" style="display: none">
<iframe id="iframeCheckPatient" class="csstable" runat="server" width="550px" height="485px" scrolling="auto">
</iframe>
<table>
<tr>
<td align="right">
<asp:Button ID="btnClose" runat="server" CssClass="cssbutton" Text="Close" />
</td>
</tr>
</table>
</div>
Serve Code
protected void btnCheckPatientID_Click(object sender, EventArgs e)
{
iframeCheckPatient.Attributes["src"] = "Check_Patient.aspx?patientID=" + txtPatientID.Text.Trim();
btnCheckPatientID_ModalPopupExtender.Show();
}
I have open an Ajax Modal popup on click of btnCheckPatientID.
In Modal Popup load an iframe which contains Check_Patient.aspx page.All working correctly.
In Check_Patient.aspx there is Pass button.
On click of button i have to return some value to parent page.
What should i do.
Search on google but cant find any help
You can use button postbackurl property where you need to specify your parent page address and on the parent page use the following code:
let say you have a textbox on your popup, you can use the same as folows
TextBox txtnew=(TextBox)PreviousPage.FindControl("id or name of control");
and then use its value OR create a property on popup page like
public string popupdata
{
get; set;
}
popupdata=your poppage value;
now access the same on parent page using
string str=PreviousPage.popupData.Tostring();
OR you can use cookies or session as your other options.

How to show the Pop up from when click theASP calender date?

I'm using ASP calender. I want to show a popup when a user clicks the date,also I want to show the selected date in the popup. I used this link.
This is my asp calender
<div id='basic-modal'>
<asp:Calendar ID="cal" Width="700" OnDayRender="DayRender" BackColor="white" runat="server" OnSelectionChanged="cal_SelectionChanged" >
<SelectedDayStyle BackColor="#C00000" />
<DayStyle CssClass="basic" />
</asp:Calendar>
this is my pop up form.
<div id="basic-modal-content">
<table>
<tr><td>Date:</td><td>
<asp:Label ID="lblSelectDate" runat="server" Text=""></asp:Label></td>
</tr>
<tr><td>Message : </td><td>
<textarea id="txtMessage" cols="20" rows="2" ></textarea>
</td>
</tr>
<tr><td></td><td><input type="submit" value="Submit" /></td></tr>
</table>
</div>
when I click the date it shows popup , but It doesn't call the "cal_SelectionChanged" function.
protected void cal_SelectionChanged(object sender, EventArgs e)
{
lblSelectDate.Text = e.ToString();
}
So my problem is how to pass the selected date to popup?
This is a working code..
protected void cal_SelectionChanged(object sender, EventArgs e)
{
lblSelectDate.Text = cal.SelectedDate.ToString();
}

Refreshing a Repeater control in an UpdatePanel with ASP.NET

I'm trying to code a page where you can post a comment without reloading the whole page. The comments are displayed using a Repeater control. The template looks like this:
<asp:UpdatePanel runat="server" ID="commentsUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<!-- Comments block -->
<div class="wrapper bloc content">
<h3><img src="img/comments.png" alt="Comments" /> Comments</h3>
<p><asp:Label ID="viewImageNoComments" runat="server" /></p>
<asp:Repeater ID="viewImageCommentsRepeater" runat="server">
<HeaderTemplate>
<div class="float_box marge wrapper comments">
</HeaderTemplate>
<ItemTemplate>
<div class="grid_25">
<span class="user"><%#Eval("username")%></span><br />
<span style="font-size:x-small; color:#666"><%#Eval("datetime") %></span>
</div>
<div class="grid_75">
<p align="justify"><%#Eval("com_text") %></p>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
<!-- Post comment block -->
<div class="wrapper bloc content">
<h3><a id="post_comment" name="post_comment"><img src="img/comment_edit.png" alt="Comments" /></a> Post
a comment</h3>
<p class="description">Please be polite.</p>
<p>
<asp:Label ID="postCommentFeedback" runat="server" />
</p>
<table border="0">
<tr>
<td valign="top">
<asp:TextBox id="postCommentContent" runat="server" TextMode="MultiLine"
MaxLength="600" Columns="50" Rows="15" Width="400px" />
</td>
<td valign="top">
<span style="font-size:x-small">BBCode is enabled. Usage :<br />
<b>bold</b> : [b]bold[/b]<br />
<i>italic</i> : [i]italic[/i]<br />
<span class="style1">underline</span> : [u]underline[/u]<br />
Link : [url=http://...]Link name[/url]<br />
Quote : [quote=username]blah blah blah[/quote]</span>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="postCommentButton" runat="server" Text="Submit"
onclick="postCommentButton_Click" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The postCommentButton_Click() function works just fine - clicking "Submit" will make the post. However, I need to completely reload the page in order to see new comments - the post the user just made will not show until then. I Databind the Repeater in Page_Load() after a (!isPostBack) check.
The postCommentButton_Click() function looks like this:
protected void postCommentButton_Click(object sender, EventArgs e)
{
// We check if user is authenticated
if (User.Identity.IsAuthenticated)
{
// Attempt to run query
if (Wb.Posts.DoPost(postCommentContent.Text, Request.QueryString["imageid"].ToString(), User.Identity.Name, Request.UserHostAddress))
{
postCommentFeedback.Text = "Your post was sucessful.";
postCommentContent.Text = "";
}
else
{
postCommentFeedback.Text = "There was a problem with your post.<br />";
}
}
// CAPTCHA handling if user is not authenticated
else
{
// CAPTCHA
}
}
In my case, we do see postCommentFeedback.Text refreshed, but, again, not the content of the repeater which should have one more post.
What is it I'm missing?
You should DataBind in the Page_Load within a !IsPostBack as you are. You should ALSO databind in your Click event.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.DataBind();
}
}
protected void MyButton_Click(object sender, EventArgs e)
{
//Code to do stuff here...
//Re DataBind
this.DataBind();
}
public override void DataBind()
{
//Databinding logic here
}
Instead of making your datasource a MySqlDataReader, have your reader populate a BindingList or something like that. Keep that in session and do your databind every non-postback and click. When your user posts, you can either add it to the list and wait for something to tell it to save that, but it makes more sense in the context of posting comments to save their post to your db and redo your datapull and stomp over your BindingList and re-Databind.
Also, personal peeve: I dislike <%#Eval....%>. Code in your page is usually a bad sign. Try using the Repeater.ItemDataBound Event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
It sounds to me like the quick fix is to bind on page load regardless of postback. Alternatively, you could rebind from within postCommentButton_Click.
protected void Timer1_Tick(object sender, EventArgs e)
{
Repeater1.DataBind();
/*This is all I did for it to work.*/
}
protected void Buttontextbox_Click(object sender, EventArgs e)
{
this.DataBind();
/*Leave sql connection to your database above "this.databind"*/
}
try putting the update panel between tags and if you have already done that then check if the closing of div tags is proper

Categories