473,545 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pls Help: Datagrid the solution?

I have an ASP.NET form that allows people to answer survey questions. There
could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously).
The questions are in a db.

Is a datagrid the best way to display this data? What do you suggest?

After they enter the info I want to be able to save it back to the db.

Any helpful links/examples?

Thanks.
Nov 18 '05 #1
5 1466
The grid is a good candidate for this sort of work. Let's assume your data
has a "QuestionId ", and "Question" field, your datagrid would look something
like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />

with a save button at the bottom. In your save button click code, you would
loop through each item of the grid and pull out the QuestionID (note that's
the DataKeyField of the grid) and the answer (which will be in teh control
"txt"):

For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex))
Dim answerBox As TextBox = CType(item.Find Control("txt"), TextBox)
If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's dynamic
with the question), it gets a little more complicated. Basically, we'd add
a "TypeID" column to our data, we'd remove the textbox from our grid and put
a placeholder instead. Then, in the ItemDataBound event we'd do something
like:

Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem, DataRowView)
Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
I have an ASP.NET form that allows people to answer survey questions. There could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously).
The questions are in a db.

Is a datagrid the best way to display this data? What do you suggest?

After they enter the info I want to be able to save it back to the db.

Any helpful links/examples?

Thanks.

Nov 18 '05 #2
Karl for president! Thanks so much. I will try this out.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eF******** ******@TK2MSFTN GP09.phx.gbl...
The grid is a good candidate for this sort of work. Let's assume your data has a "QuestionId ", and "Question" field, your datagrid would look something like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />

with a save button at the bottom. In your save button click code, you would loop through each item of the grid and pull out the QuestionID (note that's the DataKeyField of the grid) and the answer (which will be in teh control
"txt"):

For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex))
Dim answerBox As TextBox = CType(item.Find Control("txt"), TextBox) If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's dynamic with the question), it gets a little more complicated. Basically, we'd add a "TypeID" column to our data, we'd remove the textbox from our grid and put a placeholder instead. Then, in the ItemDataBound event we'd do something
like:

Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem, DataRowView)
Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
I have an ASP.NET form that allows people to answer survey questions.

There
could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously).
The questions are in a db.

Is a datagrid the best way to display this data? What do you suggest?

After they enter the info I want to be able to save it back to the db.

Any helpful links/examples?

Thanks.


Nov 18 '05 #3
AnswerBox.Text always returns "" even if there are values in it. Any ideas
why? Everything else seems ok.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eF******** ******@TK2MSFTN GP09.phx.gbl...
The grid is a good candidate for this sort of work. Let's assume your data has a "QuestionId ", and "Question" field, your datagrid would look something like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />

with a save button at the bottom. In your save button click code, you would loop through each item of the grid and pull out the QuestionID (note that's the DataKeyField of the grid) and the answer (which will be in teh control
"txt"):

For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex))
Dim answerBox As TextBox = CType(item.Find Control("txt"), TextBox) If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's dynamic with the question), it gets a little more complicated. Basically, we'd add a "TypeID" column to our data, we'd remove the textbox from our grid and put a placeholder instead. Then, in the ItemDataBound event we'd do something
like:

Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem, DataRowView)
Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
I have an ASP.NET form that allows people to answer survey questions.

There
could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously).
The questions are in a db.

Is a datagrid the best way to display this data? What do you suggest?

After they enter the info I want to be able to save it back to the db.

Any helpful links/examples?

Thanks.


Nov 18 '05 #4
You are rebinding your grid on postback.

Wherever you have your grid.DataSource = xxx and grid.Databind() commands,
wrap it in an if NOT Page.IsPostback Then ... end if

karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:Oe******** *****@TK2MSFTNG P11.phx.gbl...
AnswerBox.Text always returns "" even if there are values in it. Any ideas why? Everything else seems ok.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eF******** ******@TK2MSFTN GP09.phx.gbl...
The grid is a good candidate for this sort of work. Let's assume your data
has a "QuestionId ", and "Question" field, your datagrid would look

something
like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />

with a save button at the bottom. In your save button click code, you

would
loop through each item of the grid and pull out the QuestionID (note

that's
the DataKeyField of the grid) and the answer (which will be in teh control "txt"):

For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex))
Dim answerBox As TextBox = CType(item.Find Control("txt"),

TextBox)
If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's

dynamic
with the question), it gets a little more complicated. Basically, we'd add
a "TypeID" column to our data, we'd remove the textbox from our grid and

put
a placeholder instead. Then, in the ItemDataBound event we'd do

something like:

Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem, DataRowView)
Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
I have an ASP.NET form that allows people to answer survey questions.

There
could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously). The questions are in a db.

Is a datagrid the best way to display this data? What do you suggest?

After they enter the info I want to be able to save it back to the db.

Any helpful links/examples?

Thanks.



Nov 18 '05 #5
Thanks so much Karl!!!!!!

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:Om******** *****@tk2msftng p13.phx.gbl...
You are rebinding your grid on postback.

Wherever you have your grid.DataSource = xxx and grid.Databind() commands, wrap it in an if NOT Page.IsPostback Then ... end if

karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:Oe******** *****@TK2MSFTNG P11.phx.gbl...
AnswerBox.Text always returns "" even if there are values in it. Any ideas
why? Everything else seems ok.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eF******** ******@TK2MSFTN GP09.phx.gbl...
The grid is a good candidate for this sort of work. Let's assume your

data
has a "QuestionId ", and "Question" field, your datagrid would look

something
like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />

with a save button at the bottom. In your save button click code, you

would
loop through each item of the grid and pull out the QuestionID (note

that's
the DataKeyField of the grid) and the answer (which will be in teh control "txt"):

For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex)) Dim answerBox As TextBox = CType(item.Find Control("txt"),

TextBox)
If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's

dynamic
with the question), it gets a little more complicated. Basically, we'd
add
a "TypeID" column to our data, we'd remove the textbox from our grid
and put
a placeholder instead. Then, in the ItemDataBound event we'd do

something like:

Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem,
DataRowView) Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl

"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
> I have an ASP.NET form that allows people to answer survey questions. There
> could be from 5 to an unlimited amt of questions, depending on which
> questions the admin wants to users to answer (he sets it up

previously). > The questions are in a db.
>
> Is a datagrid the best way to display this data? What do you suggest? >
> After they enter the info I want to be able to save it back to the db. >
> Any helpful links/examples?
>
> Thanks.
>
>



Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3621
by: CGuy | last post by:
Hi, I have an ASPX page which has a datagrid and this datagrid is bound to a Custom Collection. sample code this.DataGrid1.DataSource = UserManager.Users; this.DataGrid1.DataBind();
3
1901
by: Fortra | last post by:
I'm having trouble with the UpdateCommand event with the DataGrid control. The event is wired up in the InitializeComponents properly, this.sitesDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler (this.sitesDataGrid_UpdateCommand); but when the "Update" button is clicked, the eventhandler is never...
8
3123
by: pei_world | last post by:
Hi, there; I have a problem with my datagrid control. I declared it in one of my form, set with DataGridTalbeStyle as well, and when I click on button, I would like to retrive Data from Database and bind these data to my datagrid in the form, when I click the button first time, it work fine. but when I click the second button. it report...
1
1321
by: Maqsood Ahmed | last post by:
Hello! There is a DataGrid on a form that is populated through DataTable and filters have been applied to through DataView. The DataTable gets updated through some events and obviously it also reflects at the datagrid. Now problem arises, that whenever a new record is being inserted in the datagrid, It scrolls waywardly, that distracts the...
0
1161
by: MrNobody | last post by:
I am desperately in need of some help to get a summary row for my DataGrid. A summary row is just a row always on the bottom which has totals for certain columns. I have been able to find tutorials online that show exactly how to accomplish this using the Web UI DataGrid in ASP.net, but unfortunately the DataGrids are simply not the same- the...
3
4896
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls are Web Control & not HTML control. One of these buttons whose title is Delete is added on the aspx page in design view & also I double clicked on...
4
353
by: momo | last post by:
Hello all, I need help with this. I have a datagrid (dg1) with checkboxes and I want when some rows are selected to populate another datagrid (dg2) with the selected rows from (dg1). Thanks Momo
1
1547
by: Gene Hubert | last post by:
I'm looking to use a datagrid in an odd way and not making a lot of progress. I want to read in a text file and then allow a user to arbitraily divide the data into columns. I thought I might be able to do this with a datagrid by capturing mouse events and then adding/dropping/modifying columns and then redisplaying the data. So far, I'm...
2
2206
by: Hexman | last post by:
Off on another journey through vb.net. What I want to do now is have a tab-control with 1 to 8 tab-pages for categories. I've read where there is not a hide method so I guess I'll have to tabpage.remove and tabpage.add at execution time. On each tab-page I will have a datagrid (parent) and a couple of other controls (radio buttons,...
2
1440
by: settyv | last post by:
Hi, I have webform which has 4 Datagrid controls with (built-in Pagination) and 4 search buttons to perform the search operation.When i click on search operation it is displaying the results in grid .when i click next it is telling error...Please let me know how to solve this problem. Below method can be used for all the datagrids.
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.