Click here to Skip to main content
15,925,010 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: How do I access user information? Pin
bamapookie3-Jan-07 5:19
bamapookie3-Jan-07 5:19 
QuestionSubmit the form when Hits ENTER key Pin
Jay_se3-Jan-07 4:41
Jay_se3-Jan-07 4:41 
AnswerRe: Submit the form when Hits ENTER key Pin
Pete O'Hanlon3-Jan-07 5:08
mvePete O'Hanlon3-Jan-07 5:08 
AnswerRe: Submit the form when Hits ENTER key Pin
Jon Sagara3-Jan-07 6:39
Jon Sagara3-Jan-07 6:39 
AnswerRe: Submit the form when Hits ENTER key Pin
Deepak the Cool4-Jan-07 0:30
Deepak the Cool4-Jan-07 0:30 
QuestionValidation Pin
DanB19833-Jan-07 4:12
DanB19833-Jan-07 4:12 
AnswerRe: Validation Pin
Pete O'Hanlon3-Jan-07 5:14
mvePete O'Hanlon3-Jan-07 5:14 
QuestionHelp!! Event handler fires everytime I refresh the page! Pin
Grapes-R-Fun3-Jan-07 3:36
Grapes-R-Fun3-Jan-07 3:36 
Hello friends

Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser Wink | ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code:

********Asp net Page*******

<form id="form1" runat="server">
<asp:DropDownList ID="roleDropDownList" runat="server" TabIndex="1">
<asp:ListItem>Doctor</asp:ListItem>
<asp:ListItem>Nurse</asp:ListItem>
<asp:ListItem>Engineer</asp:ListItem>
<asp:ListItem>Teacher</asp:ListItem>
<asp:ListItem>Clerk</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DepDropDownList" runat="server" TabIndex="2">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="addButton" runat="server" OnClick="addButton_Click" Text="Add Role" TabIndex="3" /><br />
<asp:GridView ID="GridView2" runat="server" ShowFooter="True" OnRowDeleting="GridView2_RowDeleting" Width="400px" EmptyDataText="Currently there are no roles entered in this area..." TabIndex="4">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>



*******Code Behind******

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class TEST_testingGridView : System.Web.UI.Page
{
DataSet ds;
DataTable dt;
DataRow dr;
StringBuilder sb;

protected void Page_Load(object sender, EventArgs e)
{
if (Session["smeRoles"] != null)
{
GridView2.DataSource = (DataSet)(Session["smeRoles"]);
GridView2.DataBind();
}
}

protected void addButton_Click(object sender, EventArgs e)
{

if (Session["smeRoles"] != null)
{
ds = (DataSet)(Session["smeRoles"]);
dt = ds.Tables["Roles"];
int rows = ds.Tables["Roles"].Rows.Count;

DataRow dr1 = dt.NewRow();
dr1["Role"] = roleDropDownList.SelectedValue;
dr1["Deployable"] = DepDropDownList.SelectedValue;

ds.Tables["Roles"].Rows.Add(dr1);
GridView2.DataSource = ds.Tables["Roles"];
GridView2.DataBind();

Session["smeRoles"] = ds;

dt.Dispose();
ds.Dispose();
}
else
{
ds = new DataSet();
DataTable dt = new DataTable("Roles");

DataColumn dc1 = new DataColumn("Role", typeof(string));
DataColumn dc2 = new DataColumn("Deployable", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);

DataRow dr1 = dt.NewRow();
dr1["Role"] = roleDropDownList.SelectedValue;
dr1["Deployable"] = DepDropDownList.SelectedValue;

dt.Rows.Add(dr1);
ds.Tables.Add(dt);

GridView2.DataSource = ds.Tables["Roles"];
GridView2.DataBind();

Session["smeRoles"] = ds;

dt.Dispose();
ds.Dispose();
}

generateOutput(); //for testing

}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int deleteIndex = e.RowIndex;
ds = (DataSet)(Session["smeRoles"]);
ds.Tables["Roles"].Rows[deleteIndex].Delete();
Session["smeRoles"] = ds;
GridView2.DataSource = ds;
GridView2.DataBind();
ds.Dispose();

generateOutput(); //for testing
}

/// <summary>
/// For testing...
/// </summary>
private void generateOutput()
{
sb = new StringBuilder();
for (int i = 0; i < GridView2.Rows.Count; i++)
{
sb.Append(GridView2.Rows[i].Cells[1].Text + ", " + GridView2.Rows[i].Cells[2].Text + "\n");
}
Response.Write(sb.ToString());
sb.Remove(0, sb.Length);
}
}


Everything works ok, except that the event handler for the addRoles button fires everytime I refresh the page!!! However, if I trigger the event handler wired to the delete LinkButton within the GridView (or any other event handler for that matter) and then refresh the page I do not have this problem. Can anyone tell me what’s going on?!? Am I missing something? I must be, because this is the first time I have seen such thing!

Many thanks in advance.


Nila
AnswerRe: Help!! Event handler fires everytime I refresh the page! Pin
Pete O'Hanlon3-Jan-07 5:11
mvePete O'Hanlon3-Jan-07 5:11 
AnswerRe: Help!! Event handler fires everytime I refresh the page! Pin
Paszt3-Jan-07 9:53
Paszt3-Jan-07 9:53 
GeneralRe: Help!! Event handler fires everytime I refresh the page! Pin
Grapes-R-Fun3-Jan-07 10:01
Grapes-R-Fun3-Jan-07 10:01 
AnswerRe: Help!! Event handler fires everytime I refresh the page! Pin
nagendra rao s.v.3-Jan-07 18:47
nagendra rao s.v.3-Jan-07 18:47 
GeneralRe: Help!! Event handler fires everytime I refresh the page! Pin
Grapes-R-Fun4-Jan-07 6:10
Grapes-R-Fun4-Jan-07 6:10 
GeneralRe: Help!! Event handler fires everytime I refresh the page! Pin
nagendra rao s.v.4-Jan-07 16:47
nagendra rao s.v.4-Jan-07 16:47 
GeneralRe: Help!! Event handler fires everytime I refresh the page! Pin
Grapes-R-Fun5-Jan-07 4:51
Grapes-R-Fun5-Jan-07 4:51 
AnswerRe: Help!! Event handler fires everytime I refresh the page! Pin
Deepak the Cool4-Jan-07 0:43
Deepak the Cool4-Jan-07 0:43 
GeneralRe: Help!! Event handler fires everytime I refresh the page! Pin
Grapes-R-Fun4-Jan-07 6:07
Grapes-R-Fun4-Jan-07 6:07 
Questionprolem with dropdownlist_selectedindex Pin
mohd imran abdul aziz3-Jan-07 3:30
mohd imran abdul aziz3-Jan-07 3:30 
AnswerRe: prolem with dropdownlist_selectedindex Pin
Not Active3-Jan-07 3:58
mentorNot Active3-Jan-07 3:58 
AnswerRe: prolem with dropdownlist_selectedindex Pin
ednrgc3-Jan-07 4:37
ednrgc3-Jan-07 4:37 
AnswerRe: prolem with dropdownlist_selectedindex Pin
Deepak the Cool4-Jan-07 0:47
Deepak the Cool4-Jan-07 0:47 
Questioncontact form Pin
sacheesach3-Jan-07 1:55
sacheesach3-Jan-07 1:55 
AnswerRe: contact form Pin
varshavmane3-Jan-07 1:59
varshavmane3-Jan-07 1:59 
GeneralRe: contact form Pin
sacheesach3-Jan-07 2:11
sacheesach3-Jan-07 2:11 
GeneralRe: contact form Pin
Not Active3-Jan-07 2:13
mentorNot Active3-Jan-07 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.