My goal is to have a input page where I have a series of controls (drop down, text box, date field) in a row. I want to have multiple rows with each row being a new record.
I think I am braindead today, because this concept if all of a sudden boggling me.
If I have a preset form with say 10 rows, how would I set it up so when I press a (submit) button, it starts loading each row (with information) as a record?
I have something similiar set up with FileUpload fields. I have a loop that looks for the control (all named IMAGE1, IMAGE2, IMAGE3, etc and if a file exists, then it inserts the image information into the database. However, that is for 1 field. I have 4 fields I need to add.
I suppose I could do something similiar, doing a FindControl for the first field and seeing if it has something other than the default, and since there is a "count" for each control, if the FindControl finds information, then it will add each field NAME1, DATE1, DETAILS1, ACTIVITY1.... and if not, the loop ends..
Does that make sense? or is it just senseless rambling?
Ok.. I figured this out.. I think... The problem I am having is I am not sure how to determine the value of the control.
If I name the controls something standard like
EVENT1 NAME1 DATE1 DETAILS1
EVENT1 NAME2 DATE2 DETAILS2
I should be able to loop it by incrementing the loop. However, I am not sure how to get the text of the control name when its being generated by the loop. (see below). Any thoughts on how I could get that information so I use it? I have the Name1, but how do I use this to get Name1.txt for example? I am convinced that I am either braindead and this is the simplest concept I am missing, or I am looking at this wholly the wrong way.
my code looks like this
int StartCount = 1;int Totalcount = 10;
while (StartCount <= Totalcount){
string namecontrolname ="theName" + StartCount;string datecontrolname ="theDate" + StartCount;
string eventcontrolname ="theEvent" + StartCount;string detailscontrolname ="theDetails" + StartCount;
string EV ="Event" + StartCount;
(I need the loop below to make it all work, but I dont know how to get the value)
if (EV.txt != the default text){ enter insert query code here and increment loop}
Hi,
Based on my understanding, you want to get the value of the control dynamic in page. If I have misunderstood you, please feel free to tell me, thanks.
As far as I know, you can achieve it by FindControl. There are several approachs can help you.
1. Using FindControl directly. This is simplest approach.
int i;for (i=0;i<10;i++) { TextBox txt = (TextBox)Page.FindControl("ControlName" + i.ToString());if (txt.Text =="default text") { } }2. Another way is using "Page.Controls.Count" to get the count of controls in page, and it needs verify whether Page.Control.Item[i] is TextBox or not.
3. You can also use Recursion Approach to find the control in page. This approach is best but most complicated.
The second and third approach you can get at following link:
http://forums.asp.net/t/1190830.aspx
Hope this can help.
Hi,
This could be very easily achieved either by using a gridview or a repeater control. The controls dynamically generate by themselves if you are adding a new row, and the control ID can be easily retrieved using the findControl method. Did you try that approach?
Thanks,
Arjun
Hi bjsusol,
If you are trying to insert multiple items at a time then you can have it this way
Using system.Text;
String sql = String.Empty;
for (int i = 0; i < 10; i++)
{
TextBox TxtName = (TextBox)this.Page.FindControl("Textbox1");
TextBox TxtDate = (TextBox)this.Page.FindControl("Textbox2");
TextBox TxtEvent = (TextBox)this.Page.FindControl("Textbox3");
TextBox TxtDetails = (TextBox)this.Page.FindControl("Textbox4");
// and so on.......
String Name = TxtName.Text;
DateTime Date =Convert.ToDateTime(TxtDate.Text).ToShortDateString();
String Event = TxtEvent.Text;
String Details = TxtDetails.Text;
sb.AppendFormat("'{0}','{1}', '{2}', '{3}'", Name , TxtDate , TxtEvent , TxtDetails);
sql += string.Format("INSERT INTO TableName (Field1 ,Field2 , Field3 , Field4)VALUES ({0});", sb.ToString());
SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING")
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteScalar();
cmd.Connection.Close();
conn.Close();
}
This will do the trick
I appreciate the suggestions. I did not notice them until just a short while ago.
I will definitely be looking them over this evening and seeing if I can get it going.
I appreciate the assist!
0 comments:
Post a Comment