Multiple Button in a single view - asp.net

I have a web project which includes two button in a view.when i click a button some textboxes along with that second button have to be visible.After entering data in textboxes,when i am trying to click the second button, its not working.What should i do to make it work?
Thanks in advance.

Rahul try this
In aspx page
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
In aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button2.Visible = false;
TextBox1.Visible = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button2.Visible = true;
TextBox1.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "Testing";
}

Related

Disable postback of a dynamically created button asp.net

This is my Page1.aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Panel>
</asp:Content>
This is my Page1.aspx.cs code
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
btn_Click(sender, e);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Dynamic button";
btn.Click += btn_Click;
Panel1.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Response.Redirect("../Page2");
}
}
From this code, when "Button1" is clicked it should create a new button "btn" and when "btn" is clicked, it should go to the url in its click function (i.e. btn_Click()). But when I click "Button1" it does not execute "Button1_Click()" but "btn_Click()". What should I do??
if (IsPostBack == true)
can become
if (IsPostBack)
Then
Response.Redirect("../Page2");
can become
Response.Redirect("~/Page2");
In the aspx check you have:
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click"></asp:Button>
Moreover you can bind click in code behind (eg OnLoad):
Button1.Click += Button1_Click;
and also unbind:
Button1.Click -= Button1_Click;

asp gridview button another command

I have a Gridview based on a Acces DB in .aspx
I added +1 column to the grid, which is:
<asp:TemplateField HeaderText="view">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Select" CommandName="Select" CausesValidation="False" id="Button1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I have a button outside of the grid: Button2
Could I add a command to Button1, to simulate to click on Button2 as well? Thank you, regards.
Did I get you correctly:
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Select")
{
MyFunction();
}
}
void Button2_Click(Object sender, EventArgs e)
{
MyFunction();
}
void MyFunction(){
//your code
}

File upload button for each row inside gridview and button to upload in asp.net

I need to have file upload button for each row in grid view. Finally I need a button to upload the files selected from fileupload button. I have a code, but it has both file upload button and button to update, however if I change the command name it is not working. Below is my code.
In my code I am displaying both fileupload buttons and buton updates. This works fine, but I need multiple file upload buttons with single button to update all the files.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Container.DataItemIndex %>'
Text="Upload" OnClick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] strArray = { "Test1", "Test2", "Test3" };
GridView1.DataSource = strArray;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int index = int.Parse(((Button)sender).CommandArgument);
FileUpload file = (FileUpload)GridView1.Rows[index].FindControl("FileUpload1");
if (file != null)
{
if (file.HasFile)
{
Response.Write(file.PostedFile.FileName);
Response.End();
//file.SaveAs(Server.MapPath("~") + "\\DataBind\\" + System.IO.Path.GetFileName(file.PostedFile.FileName));
}
}
}
It looks like you're close. You just want to do the same thing on all the rows. You can use a foreach loop for that:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
FileUpload file = (FileUpload)row.FindControl("FileUpload1");
if (file != null)
{
if (file.HasFile)
{
// Save your file here
}
}
}
}

C# - Event handler for dynamic buttons

I have a textbox and submit button created using the design mode.
When the submit button is pressed, it will retrieve the user input from the textbox and then make a query to my database.
It will then display a list of dynamic buttons according to the information retrieved from my database.
However, the event handler for the buttons does not fire when clicked. I guess my problem is the postback but I cannot create those buttons in page_load etc. because I need to get the user input (from the textbox when submit button is pressed) before i can load the buttons.
How can i solve this problem?
Thank you.
Edit (codes):
protected void subBtn_Click(object sender, EventArgs e)
{
//database setup codes
.......
while (reader.Read())
{
Button detailsBtn = new Button();
detailsBtn.Text = reader["fName"].ToString();
//doesn't fire
detailsBtn.Click += new EventHandler(detailsBtn_Click);
memPanel.Controls.Add(detailsBtn);
}
}
Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists.
For quick demo see this code
ASPX CODE
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>
ASPX.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
generate();
}
}
public void generate()
{
if (!pnl.HasControls())
{
for (int i = 0; i < 4; i++)
{
Button detailsBtn = new Button();
detailsBtn.Text = "fName" + i.ToString();
detailsBtn.ID = i.ToString();
detailsBtn.Click += new EventHandler(detailsBtn_Click);
pnl.Controls.Add(detailsBtn);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
generate();
}
protected void detailsBtn_Click(object sender, EventArgs e)
{
}
Sound to me like you could easily refactor your page to use a simple <asp:Repeater runat="server" ..></asp:Repeater> instead of dynamically adding controls to a Panel.
Here is a very simple complete sample:
RepeaterTest.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TextBox ID="theTextBox" runat="server"></asp:TextBox>
<asp:Button ID="theButton" runat="server" OnClick="theButton_Click" Text="Click me" />
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<asp:Button ID="theRepeaterButton" runat="server" Text='<%# Eval("fName") %>' OnClick="theRepeaterButton_Click" />
</ItemTemplate>
</asp:Repeater>
</asp:Content>
RepeaterTest.aspx.cs
public partial class RepeaterTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void theButton_Click(object sender, EventArgs e)
{
string filter = theTextBox.Text;
// below row simulates fetching data using the filter text in the text box
var data = Enumerable.Range(0, 20).Select(i => new { fName = filter + " " + i });
test.DataSource = data;
test.DataBind();
}
protected void theRepeaterButton_Click(object sender, EventArgs e)
{
var button = (Button)sender;
// do something here based on text/commandname/commandargument etc of the button
}
}

Populate different gridview by clicking different button

In my web page header, there are several buttons. What I want is to click each button to display a desired table in a gridview.
click button1
Display table in gridview 1.
click button2 Display table in gridview 2, Gridview 1 disappeared.
The header and buttons are persisting in the page. I don't want to redirect the current page to different pages. I only want to implement the function in one web page.
ASPX
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</div>
C#
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
GridView2.Visible = !GridView1.Visible;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
GridView2.Visible = !GridView1.Visible;
GridView2.DataBind();
}
You may need to pass in the .DataBind() call depending on how you are binding your data to the gridviews.
On each button click just bind your GridView as needed. For example:
Button1 clicked do:
protected void Button1_OnClick(object sender, EventArgs e)
{
grdYourGrid1.DataSource = yourDataSource;
grdYourGrid1.DataBind();
grdYourGrid1.Visible = true;
grdYourGrid2.DataSource = null;
grdYourGrid2.DataBind();
grdYourGrid2.Visible = false;
}
Then do the opposite for Button2. I suggest rebinding and nulling the non visible grid so the ViewState doesn't get bloated when not required.
You can put this in the Page_Load
button1.Click+=(s,ev)=>
{
gridview1.visible = true;
gridview1.DataBind();
};
button2.Click+=(s,ev)=>
{
gridview2.DataBind();
gridview1.visible = false;
};
C#
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.visible = true; gridview1.DataBind(); gridview2.visible = false;}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.visible = true; gridview2.DataBind(); gridview1.visible = false;}
or
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.DataBind(); gridview2.Rows.Clear();}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.DataBind(); gridview1.Rows.Clear();}

Resources