textBox.text is empty in onClick function Server side - c#

I have a very simple asp.net page and something not working as it should be.
This is the structure of my page:
<%# Page Title="" Language="C#" MasterPageFile="~/Management/page.master" AutoEventWireup="true" CodeFile="Account.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<div class="contentWrapper">
<asp:Panel runat="server" ID="MainPanel" ClientIDMode="Static" CssClass="MainPanel" OnLoad="MainPanel_Load">
..........
..........
..........
..........
..........
..........
<asp:Panel runat="server" id="PasswordHolder" CssClass="rtl" Width="100%" ClientIDMode="Static">
<asp:TextBox runat="server" ID="PasswordInput" CssClass="block" TextMode="Password" />
<asp:LinkButton runat="server" CssClass="btn ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" ID="EditBtn" OnClick="EditBtn_Click"><span class="ui-button-text">submit</span></asp:LinkButton>
</asp:Panel>
</asp:Panel>
</asp:Content>
Now: I'm entering a text in the TextBox(PasswordInput) and clicking submit.
The Server catch the event and entering EditBtn_Click function.
BUT! PasswordInput.Text is empty.
What should I do to fix that?
Server Side Code:
protected void Page_Load(object sender, EventArgs e)
{
if (currentUser() == null)
{
Response.Redirect("Login.aspx", true);
}
if (!IsPostBack)
{
ViewState["ContactsSortExpression"] = "LastName";
ViewState["ContactsSortDirection"] = "ASC";
}
}
protected void EditBtn_Click(object sender, EventArgs e)
{
if (PasswordInput.Text == currentUser().Password)
{
//Do something
}
}

My Problem was the Jquery dialog function
For an answer look here:
asp.net Textbox value is null when post bock with Jquery Dialog occured

Related

ASP.NET & C# : gridview select does no action

Why is GridView1_SelectedIndexChanged not working?
I'm learning ASP.NET on my own (which means I learn through youtube), using VS 2015.
I created a grid of items and want to show the selected item in a textbox, but code doesn't do any change.
I created an empty project, added a masterpage and default page, and just added a text and grid, scriptmanager updatepanel contenttemplate.
Whenever I click select on any row from the grid textbox is still empty.
MasterPage.master:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default.aspx:
<%# Page Title="" enableeventvalidation="true" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="auto-style1">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Default.aspx.cs:
using System;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("serial");
for (int i = 0; i < 4; i++)
{
DataRow row = tbl.NewRow();
row[0] = i + 1;
tbl.Rows.Add(row);
}
GridView1.DataSource = tbl;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
}
}
You are selecting the wrong cell on the SelectedIndexChanged method.
The first Cell (index 0) contains the button, so you want the second cell (index 1).
You should place a breakpoint on the method and see for yourself what other properties are filled in the GridView.

Problem second click create dynamically elements ASP.NET WEB FORMS

It does not create the button dynamically when I click the button a second time. It only creates the button on the first click and then not. What could it be?
public partial class _Default : Page
{
static bool enable = false;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAgregarItems_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "HOLA";
PlaceHolder1.Controls.Add(button);
}
}
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ejemplo._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="container">
<asp:Button ID="btnAgregarItems" runat="server" Text="Agregar Items" CssClass="btn btn-primary my-3" OnClick="btnAgregarItems_Click" />
<div class="row">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAgregarItems" />
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>

Select2 is being duplicated in my component

I am using the jquery plugin in my pages e until now i have not any problem with it. the problem happend when i am tring using the select2 with a ascx page. More description of the problem below.
I have a aspx that i am using a component ascx to show somethings:
<%# Page Title="" Language="C#"
MaintainScrollPositionOnPostback="True" AutoEventWireup="true"
CodeBehind="Register.aspx.cs" Inherits="Organization.Register" %>
<%# Register Src="../DialogUF.ascx" TagPrefix="comp" TagName="DialogUF" %>
<asp:Content runat="server">
<asp:LinkButton ID="btnUF" runat="server" CausesValidation="False"
OnClick="btnUF_Click">aqui</asp:LinkButton>
<Comp:DialogUF ID="dlgUF" runat="server" />
</asp:Content>
In My code behind:
protected void btnUF_Click(object sender, EventArgs e)
{
dlgUF.Visible = true;
}
Mas ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="DialogUF.ascx.cs"
Inherits="Organization.DialogUF" %>
...
<asp:Panel ID="pnlBody" runat="server">
<fieldset class="grid-form">
<div data-row-span="12">
<div data-field-span="6">
<asp:Label runat="server" AssociatedControlID="ddlState">UF
<span class="text-danger">(*)</span>:</asp:Label>
<asp:DropDownList ID="ddlState" runat="server"
CssClass="form-control"
OnSelectedIndexChanged="ddState_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
</div>
</div>
</fieldset>
...
<script type="text/javascript">
$(document).ready(function () {
$("#<%= ddlState.ClientID %>").select2();
});
</script>
</asp:Panel>
Image of select2 duplicated

Loading content around TextBoxes in ASP.NET

I'm trying to create a form in ASP.NET but I'd like to know if I can dynamically add HTML content around a TextBox.
Here is the code:
<%# Page Title="" Language="C#" MasterPageFile="~/MPpacientes.master" AutoEventWireup="true" CodeFile="CuestionarioGeneral.aspx.cs" Inherits="CuestionarioGeneral" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="navbar" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="headertitle" Runat="Server">
Cuestionario General
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="contentbox" Runat="Server">
<legend>Lorem ipsum dolor sia met etcetcetc</legend>
<%-- question1 --%>
<asp:Label ID="lbl1" runat="server" Text="1"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<button class="btn-clear" tabindex="-1"></button>
</div>
<%-- question2 --%>
<asp:Label ID="lbl2" runat="server" Text="2"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<button class="btn-clear" tabindex="-1"></button>
</div>
<%-- question3 --%>
<%-- etc --%>
As you can see in the code, the content of the < div > and the < button > tags don't change at all in each question, so I wanna know if there's some way to load them from a script so that when the page loads they wrap around each TextBox automatically.
Thanks in advance.
did the total question for each page different or the same?
if different, you can try to use asp:Repeater for that. You can dynamically change the question, and keep the button and div static for every question.
for example :
<asp:Repeater ID="rptQuestion" runat="server" OnItemDataBound="rptQuestion_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblQuestion"></asp:Label>
<div class="input-control text" data-role="input-control">
<asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
<asp:Button CssClass="btn-clear" TabIndex="-1" ID="btnClear" runat="server"></button>
</div>
</ItemTemplate>
</asp:Repeater>
Edit :
to edit the Label text, you can first add all your question to List<String> then Bind it to the repeater, here's the example :
on PageLoad :
protected void Page_Load(object sender, EventArgs e)
{
List<String> listQuestion = new List<String>();
foreach(string question in ...)
{
listQuestion.Add(question);
}
rptQuestion.DataSource = listQuestion;
rptQuestion.DataBind();
}
then on the ItemDataBound event :
protected void rptQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String question = (String)e.Item.DataItem;
Label lblQuestion = (Label)e.Item.FindControl("lblQuestion");
lblQuestion.Text = question;
}
}
you don't need to change the ID for the Label and TextBox, because it will always be different for each item in the repeater
If you just need to add a question, you can a <asp:label /> tag, or even a Html label tag and make it runat="server".
And then you can add the text to the label dynamically in the code behind

How to use CalenderExtender in ContentPlaceHolder? [duplicate]

This question already has an answer here:
Ajax in UserControl
(1 answer)
Closed 8 years ago.
I am using CalenderExtender in my project inside in content place holder and calender extender is in a UserControl. This control is working in normal aspx-page but when i am dragging this control in ContentPlaceHolder then it is not working. Actually the Calender is not appearing in textBox below is my code which i used in my project.
ASPX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Calender1.ascx.cs" Inherits="Facultymanagement.Calender1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:TextBox ID="txtcalender" runat="server">
</asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="MM/dd/yyyy"
TargetControlID="txtcalender" PopupButtonID="txtcalender"></asp:CalendarExtender>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (CalendarExtender1.SelectedDate.ToString() != "")
{
txtcalender.Text = CalendarExtender1.SelectedDate.ToString();
}
}
public string TextBox1Value
{
get
{
return txtcalender.Text;
// return Convert.ToString(Calendar.SelectedDate);
}
set { txtcalender.Text = value; }
}
}
This is the place where i am trying to access the value:
protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = calender1.TextBox1Value.ToString();
}
You need to find control first
ContentPlaceHolder mpContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("ContentPlacename");
if (mpContentPlaceHolder1 != null)
{
Button btn_searsh;
btn_searsh = (Button)mpContentPlaceHolder1.FindControl("main_search");
btn_searsh.CssClass += " " + "btn-primary";//to pass the rentpage class
}
I think you must doo something wrong.
The following code works:
WebUserControl1.ascx:
<%# Control Language="C#" ClassName="WebUserControl" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<script runat="server">
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
</cc1:CalendarExtender>
Default.aspx:
<%# Page Language="C#" %>
<%# Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<uc1:WebUserControl ID="WebUserControl2" runat="server" />
<uc1:WebUserControl ID="WebUserControl3" runat="server" />
<uc1:WebUserControl ID="WebUserControl4" runat="server" />
</form>
</body>
</html>

Categories

Resources