|
Use the W3 Validator[^] against the pages. If your site is developed against .NET 2 you can also use Visual Studio 2005 to validate it.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello friends
Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser 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
|
|
|
|
|
First thing I'd do is put IsPostback in the Page_Load event. Don't bind based on the session.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I'm going to guess that the event handler for the addRoles button (or is it addButton Button?) fires when you refresh the page only if you press the button before refreshing the page (meaning on the previous page view, the button was pressed).
If this is correct: This is a common problem for page developers. When you hit refresh, the posted data is posted back; that posted data contained information telling the page to execute the button's event handler so when you hit refresh the event will be fired again.
There are several ways to overcome this, the simplest being to check the datasource to see if the data being inserted is already in the datasource, if not insert it, if so don't insert it.
|
|
|
|
|
Thanks Paszat
Although, I have to say, when I trigger some other event handler and then refresh the page data persists. But anyways- I will give your solution a try. Thanks again.
Nila
|
|
|
|
|
Hi,
I feel you are Missing some thing else. By Default any Event will not fire unless it is called implicitly. Here i want some info from you whether you debugged this with break points if so which event is called first?.
|
|
|
|
|
Nagendra,
Thanks for the debug tip. After stepping into the code, I saw that the button click event fires again when I refresh the page... very frustrating!! So to remedy it as quickly as possible, I created a small method that would check to see if the button click event would 'quilify' to be fired given user's input.
I just don't have the time to investigate this silliness- need to keep the boss happy
Thanks again!
Nila
|
|
|
|
|
Hai,
That is a Good Method of Making your self Comfortable in Office.
But you try it later and you can come across some good concepts in that area.
and if possible send me that page complete as an attachment to nagendrarao@glowtouch.com.
|
|
|
|
|
It's not about making myself comfortable; Im just trying to remedy this and move on- there is lots of pressure when you develop for an emergency operations type group.
And I agree with you on finding out exactly WHY sometimes things behave the way they do But now is not the time for that, maybe later. Let me do a couple of things, and then I will send you the code.
Thanks again!
Nila
|
|
|
|
|
just put and change according to ur need following code into
public partial class<br />
{<br />
private bool _refreshState;
private bool _isRefresh;
<br />
public bool IsRefresh
{<br />
get <br />
{<br />
return _isRefresh;
}<br />
}<br />
protected override object SaveViewState()<br />
{<br />
Session["_ISREFRESH"] = _refreshState;
object[] allstates = new object[2];
allstates[0] = base.SaveViewState();
allstates[1] = !_refreshState;
return allstates;<br />
}<br />
protected override void LoadViewState(object savedState)
{<br />
try<br />
{<br />
object[] allstates = (object[])savedState;
base.LoadViewState(allstates[0]);
_refreshState = (bool)allstates[1];<br />
_isRefresh = _refreshState == (bool)Session["_ISREFRESH"];<br />
}<br />
catch (Exception e)<br />
{<br />
}<br />
}<br />
}
and which event is called thru Refresh just put flowwowing code into
if (_isRefresh == false)<br />
{<br />
<br />
your code for event is here<br />
<br />
}
hope tht'll helps u
Deepak
Smile a Lots,Its Costs Nothing
|
|
|
|
|
Thanks Deepak!
There has got to be a bug in this, because the button click event fires all on its own when I refresh the page... so I finally decided to create a small private method that checks to see whether or not events in the button click would 'quilify' to be executed - very similar to your approach. Thanks again, don't know what I would do without all you guys' help and support!!!!
Nila
|
|
|
|
|
Dear All
i am bit new to dotnet. currently making web application using asp.net and C#(VS2005). i have dropdownlist control(Autopostback is true) on its selected index code
i have wrote dropdownlist_selectedindex as follows its works fine whenever there is more than one record(by fine i mean in debug mode, control flow through it ) but for singal record
controls does not go through it. What is a problem. Please guide me.
thanks
protected void DropDownListinquiryid_SelectedIndexChanged(object sender, EventArgs e)
{
setting = ConfigurationManager.ConnectionStrings["databaseconnection"];
if (setting != null)
{
conn = new SqlConnection(setting.ConnectionString);
conn.Open();
query = "select max(inquiryid) from followupdtl where inquiryid='"+DropDownListinquiryid.SelectedItem.Text+"'";
cmd = new SqlCommand(query, conn);
if (cmd.ExecuteScalar().ToString().Equals(""))
{
txtmaxno.Text = "01";
}
else
{
int number = int.Parse(cmd.ExecuteScalar().ToString());
number++;
if (number.ToString().Length == 1)
{
txtmaxno.Text = number.ToString("0#");
}
else
{
txtmaxno.Text = number.ToString("##");
}
}
}
}
regards
imran khan
|
|
|
|
|
I would guess that because you only have one item in the the list the selected item can't change, there is nothing to change, so the event is never triggered. Try adding a empty item at the top of the list.
only two letters away from being an asset
|
|
|
|
|
#1: A change event will never fire if there is only one choice and it's already selected. Try adding a "Select an item" in the first position.
#2: Separate out the database code into it's own function/method that returns the value returned from the DB.
#3: There is no need to call cmd.ExecuteScalar more than once. Store the value in a variable.
|
|
|
|
|
i was also fce tht problem at last i'd tht
into dropdownlist
i entered a Select field into 0 index thru properties
hope tht'll hepls u
Deepak
Smile a Lots,Its Costs Nothing
|
|
|
|
|
im making a contact form in that im maling all information to the provider. but wat method i use im not getting
plz help
|
|
|
|
|
May be u can use this :
Imports System.Net.Mail
|
|
|
|
|
i did use that but when i create my own method named as SENDMAIL it give error of namespace.
should i give u code
|
|
|
|
|
Are you sure the assembly is referenced in your project?
only two letters away from being an asset
|
|
|
|
|
|
you must import "Imports.System.Web.Mail"
Then use this code:
Dim objEmail As New MailMessage<br />
objEmail.To = "To Email"<br />
objEmail.From = "Your Email"<br />
objEmail.Subject = "Your Subject"<br />
objEmail.Body = "Hello"<br />
objEmail.Priority = MailPriority.High<br />
objEmail.BodyFormat = MailFormat.Text<br />
SmtpMail.SmtpServer = "Your SMTP Server"<br />
<br />
Try<br />
SmtpMail.Send(objEmail)<br />
Catch exc As Exception<br />
Throw exc<br />
End Try
This will help you!!
Adrian De Battista: .Net Programmer, Java Programmer and Web Designer.
|
|
|
|
|
Hello,
I have a CRecordset object for a table .I want to know the number of primary keys in a table alongwith the Column Nos.
can I use IsFieldNullable method for this purpose?
Thanks
Prithaa
|
|
|
|
|
A CRecordset object? There is no such class in the framework. Is this a C++ question?
---
Year happy = new Year(2007);
|
|
|
|
|
Guffa wrote: Is this a C++ question?
MFC if I remember correctly....oh and to answer the question, no - can't use IsFieldNullable for that, since it will give you "false positives"...seem to remeber that you need to look at the props of each in turn for a particular flag, though can't off the top of my head remember what it is...
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
And anyway, just because a field is null, it doesn't mean that you can assume that it is part of a key.
|
|
|
|