call insert through gridview
wits end im hoping to find solution from you guys.
i have a gridview with a few columns, 2 textbox templatefield,1 button template and bound to sqldatasource .
the button template will call the procedure insert_item with parameter (itemid, rcvd_qty, rate).
how can i hook up the insert_item(x,y,z) to the gridview button?
design sample
http://i51.tinypic.com/286ssp3.gif
Re: call insert through gridview
Assuming you have added an InsertCommand to you SqlDataSource, then all you need to do is set the CommandName property of your button.
Have a look at this article:
http://www.aspdotnetfaq.com/Faq/How-...ataSource.aspx
Hope that helps!
Gary
Re: call insert through gridview
@gep13 thanks it seems what im looking.
also i want to add validation on addbutton_click through javascript. i need to meet the conditions
1] Rcvd_Bal <=Bal of the active row
2] Rcvd_Rate <=PO Rate of the active row
Re: call insert through gridview
Hello,
In the TemplateField, where you have the TextBoxes, you can also had some Validator Controls. In your case, you won't be able to use an Out of the Box validator, but you could use a CustomValidator.
Gary
Re: call insert through gridview
great. thanks for the info...let me take this one at a time.
Re: call insert through gridview
Sure, just let me know if you have any follow up questions.
Gary
Re: call insert through gridview
twas a kick ass sample :D. the insert button works on fly but im getting error when using the value pass from gridview textbox.
Code:
<asp:TemplateField HeaderText="rcvd qty">
<ItemTemplate>
<asp:TextBox runat ="server" ID="rcvdqty_textbox" Text="0" Width="50"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
vb Code:
Dim rcvdqty As TextBox = TryCast(GridView2.Controls(0).Controls(0).FindControl("rcvdqty_textbox"), TextBox)
Dim _rcvdqty As New SqlParameter("@rcvdqty", SqlDbType.Float)
_rcvdqty.Direction = ParameterDirection.Input
'this line doesnt work and it returns an error
'Object reference not set to an instance of an object.
_rcvdqty.Value = rcvdqty.Text
insertParameters.Add(_rcvdqty)
heres the grid.
http://i53.tinypic.com/2lvztqd.gif
Re: call insert through gridview
Hello,
If you set a break point on this line:
Code:
_rcvdqty.Value = rcvdqty.Text
Can you confirm that rcvdqty is null/Nothing?
Gary
Re: call insert through gridview
hi gary, yes its says nothing.
so this code did not work i assume.
Code:
Dim rcvdqty As TextBox = TryCast(GridView2.Controls(0).Controls(0).FindControl("rcvdqty_textbox"), TextBox)
...still finding sample
from the post#7 screenshot i need to add data under columns rcvdID, grfID, rcvd qty and rcvd rate in the insert command but im not getting the correct row index.
Re: call insert through gridview
Hey,
Correct, that line is the problem one.
If FindControl does not find the item that you are looking for, it will return a null/Nothing.
If I were you, I would set a break point on that line, and then start your debugging. i..e what control do you get back when you do this:
Code:
GridView2.Controls(0)
Begin stepping into the code, using the watch window, and I am sure you will figure out what is going on.
Gary
Re: call insert through gridview
hey gary, i manage to find a time saver solution from different threads. please let me know if their is a need to revise the code.
vb Code:
If e.CommandName = "insert" Then
'get fired button from gridview container and return as row
Dim rowSelect As GridViewRow = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow)
'get row index
Dim rowindex As Integer = rowSelect.RowIndex
'get textbox value using rowindex at col 6
Dim txt As TextBox = DirectCast(GridView2.Rows(rowindex).Cells(6).FindControl("rcvdqty_textbox"), TextBox)
'do the rest
End if
thanks.
Re: call insert through gridview
great now i need to perform client validation. 3 rules:
1. rcvd qty is a required field. can be done through required field validation control
2. rcvd qty must be numeric-float in format. can be done through regular expression validation control
3. now comes another issue. i need to compare the rcvd qty with balance qty. its obvious rcvd qty should be always <= balance qty. i used label control for balance qty and textbox for rcvd quantity. having said that it seems the comparison validation control do not support textbox and label comparison. now the work around again to use javascript. simple and easy :D, call javascript validation function when insert button fires up.
im just wondering why im not getting the value of rcvd textbox during grid iteration. in the page source the rcvd qty has this name GridView3$ctl03$rcvdqty_textbox where ctl03 is the control index. similarly the balance label has this name series GridView3_ctl02_balanceLabel where ctl02 is the control index.
somehow im not getting the value of rcvdqty textbox.
Code:
var thetextbox;
thetextbox=document.getElementById('GridView3_ctl02_rcvdqty_textbox');
alert(thetextbox.innerHTML);
suggestions are open. thanks.
Re: call insert through gridview
Hello,
Change you code slightly to be this:
What do you get?
Gary
Re: call insert through gridview
hi gary,
got it with this code
thetextbox=document.getElementById('GridView3_ctl02_rcvdqty_textbox').value;
alert(thetextbox);
im doing the other stuff. i'll be back quickly. thanks.
Re: call insert through gridview
Cool, looks like you have it.
The reason that I was asking you to do this:
Was to prove that the getElementById was actually finding something. If it wasn't you would have got a null in the alert message. You should have been getting object, the extension from that would have been to again test:
Code:
alert(thetextbox.innerHTML);
If this returned null, then the natural conclusion would have been that the innerHTML property was not available.
Gary