Problem second click create dynamically elements ASP.NET WEB FORMS - c#

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>

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.

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

Update UI controls outside the updatepanel in asp.net

I'm trying to update controls outisde the update panel with the ref. of below link . but its not working. i want to know what i'm missing.in my application can't use these labels inside the update panel.
http://msdn.microsoft.com/en-us/library/bb301423(v=vs.110).aspx
HTML
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Updatepaneltest._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
function PageLoadingHandler(sender, args) {
var dataItems = args.get_dataItems();
if ($get('Label1') !== null)
$get('Label1').innerHTML = dataItems['Label1'];
if ($get('Label2') !== null)
$get('Label2').innerHTML = dataItems['Label2'];
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
UpdatePanel content.
<asp:Button ID="Button1" Text="Submit" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
</div>
</asp:Content>
code behind
protected void Page_Load(object sender, EventArgs e)
{
// ScriptManager ScriptManager1 = ScriptManager.GetCurrent(this.Page);
if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
{
ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
ScriptManager1.RegisterDataItem(Label2, DateTime.Now.Year.ToString());
}
}
Control updates inside of updatepanel,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" Text="Submit" runat="server" />
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Control updates outside of updatepanel,
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:Button ID="Button1" Text="Submit" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
Later, write below lines in client side.
var pageInstance = Sys.WebForms.PageRequestManager.getInstance();
pageInstance.add_pageLoaded(UpdateLabelHandler);
function UpdateLabelHandler(sender, args)
{
var ControldataItems = args.get_dataItems();
if ($get('Label1') !== null)
$get('Label1').innerHTML = ControldataItems ['Label1'];
if ($get('Label2') !== null)
$get('Label2').innerHTML = ControldataItems ['Label2'];
}
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
ScriptManager1.RegisterDataItem(Label2, DateTime.Now.Year.ToString());
}
Hope this may help you.

textBox.text is empty in onClick function Server side

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

refreshing entire page from within an updatepanel

Here's my situation. I have a web forms page, and it's getting annoying when the entire page scrolls to the top every time you click a control, so I'm trying to apply update panels to my page.
The submit button may or may not be click-able depending on whether or not the contents of individual update panels are in a proper state.
When the submit button is clicked, It can potentially affect any of the controls on the page.
I understand that I can do some of this with the <triggers> tag element of the update panel, but I don't want to have to put everything on the page into an update panel with it's own trigger, I'd rather that the submit button just reload the entire page.
For the sake of simplicity I've put together a sample project to represent my page. It has a "reset" button, to represent the "submit" button.
The Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
The default page:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label runat=server Text="This lable represents things not in update panels" ID="label1"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat=server>
<ContentTemplate>
<table>
<tr>
<td><asp:Button ID="button1" runat="server" OnClick="Button1_click" Text="Button 1" /></td>
<td><asp:TextBox ID="textBox1" runat="server" Text="StartText" Enabled="false" /></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat=server>
<ContentTemplate>
<table>
<tr>
<td><asp:Button ID="button2" runat="server" OnClick="Button2_click" Text="Button 2" /></td>
<td><asp:TextBox ID="textBox2" runat="server" Text="StartText" Enabled="false" /></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanelReset" runat=server>
<ContentTemplate>
<asp:Button ID="reset" runat="server" OnClick="Reset_click" Text="reset" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="button1" EventName="click" />
<asp:AsyncPostBackTrigger ControlID="button2" EventName="click" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
The code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_click(object sender, EventArgs e)
{
textBox1.Enabled = !textBox1.Enabled;
reset.Enabled = !textBox1.Enabled && !textBox2.Enabled;
}
protected void Button2_click(object sender, EventArgs e)
{
textBox2.Enabled = !textBox2.Enabled;
reset.Enabled = !textBox1.Enabled && !textBox2.Enabled;
}
protected void Reset_click(object sender, EventArgs e)
{
textBox1.Text = "StartText";
textBox2.Text = "StartText";
label1.Text = "reset button clicked";
}
}
}
Just add a PostBackTrigger to the last UpdatePanel.
<asp:PostBackTrigger ControlID="reset" />

Categories

Resources