using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.
Related
I have added a checkbox to the rows of a gridview that when clicked will expand a panel in the row. My gridview needs to use paging, so I am saving in a session variable the state of the current page before the page change. When the user clicks back to a page I am repopulating the checkboxes, but this does not expand the panels. Is there away to expand the panels from the code behind?
<asp:CheckBox runat="server" Text="Order Updated Records" ID="cbUR" Visible='<%# !DBNull.Value.Equals(Eval("AnyBox"))%>' />
<asp:CollapsiblePanelExtender ID="cInst" runat="server" TargetControlID="inst" Collapsed="true" AutoExpand="true" AutoCollapse="false" ExpandControlID="cbUR" CollapseControlID="cbUR" />
<asp:Panel ID="Inst" runat="server">
<asp:TextBox runat="server" ID="txtInst" TextMode="MultiLine" Width="200" />
</asp:panel>
I tried adding the panel, textbox and panel extender from the code, but could not get it to work. I read on a different post that the whole gridview would need to load from the code for this to work.
I would love to use something like
<asp:CollapsiblePanelExtender ID="cInst" runat="server" TargetControlID="inst" Collapsed='<%#!Convert.ToBoolean(rowItems[index].ToString()) %>' />
Would it be better not use the CollapsiblePanelExtender and find a different way to show the panels?
You could try adding a public/protected method:
public bool IsCollapsed(object rowId) {
//get row by ID here and return true if collapsed
return ....
}
that would return a value for a row that needs to be expanded or collapsed.
and use it like:
Collapsed='<%# IsCollapsed(Eval("RowId")) %>'
where RowId is a property that represents the ID of the item.
I've created FormView let user enter the data to save into database. And i need to disable one of the textbox and automatic set the GUID to the textbox. how can i do that?
<InsertItemTemplate>
RouteGUID:
<asp:TextBox ID="RouteGUIDTextBox" runat="server"
Text='<%# Bind("RouteGUID") %>' Enabled="False" />
<br />
you should Bind the FormView control on Page_Load..or should call the Formview.DataBind() method on page_load...as i can't see your full code, so said this as an assumption.
What I'm trying to accomplish is when I click a button in my gridview, the values of that particular row must be displayed in textlabels lblID, lblName, ...
I just don't have a clue how to do it.
This is the code of my sqldatasource:
<asp:SqlDataSource ID="srcProd" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnDB %>"
ProviderName="<%$ ConnectionStrings:ConnDB.ProviderName %>"
SelectCommand="SELECT p.tProdID, p.tProdImage, p.tProdBeschrijving, m.tMerkNaam, m.tMerkImage, p.tProdVerkoop FROM tblProducten AS p INNER JOIN tblMerken AS m ON p.tMerkID = m.tMerkID ORDER BY m.tMerkNaam">
</asp:SqlDataSource>
This is a screenshot of the gridview I'm talking about.
When I press one of the buttons I would like e.g. the parameter ID displayed in lblID.
All help, hints and tips are appreciated.
Assign a CommandName (which is like "ShowDetails" or so) and a CommandArgument (which is the ID of the record) to the button. A click on the button will now trigger the grid's RowCommand event, passing you both CommandName and CommandArgument in e. Then fetch the record from your DB and populate your controls:
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Foo2" CommandArgument='<%# Eval("RowID") %>'
CommandName="ShowDetails" />
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Debug.WriteLine("Command Handler");
}
SO basicly this can be done on two ways:
Add a server-side click handler behind the button and update the label inside an UpdatePanel (this case we do not need a postback)
Add some javascript on the button's "OnClientClick" event to set this label... This method can be set in the GridView's bind method.
Hope this helps.
I'm having an issue where the button within a radgrid is not firing until second time.
I have a usercontrol with a radgrid on it and button within one of its column.
The usercontrol is placed on a page.
When clicking the button on radgrid nothing happens 1st time, however it works 2nd time.
This is some of the radgrid column data
<telerik:GridTemplateColumn DataField="Quantity" HeaderText="Quantity" UniqueName="QuantityCol">
<ItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Columns="4" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' width="40px" />
<asp:LinkButton id='btnUpdateRow' runat="server" CausesValidation="false" Text='<span>Update</span>' CommandName="ButtonUpdateRow" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</ItemTemplate>
</telerik:GridTemplateColumn>
The radgrid uses NeedDataSource to obtain its data.
Page Load does not do anything.
During debug within itemcreated/itemdatabound, the linkbutton clientid it shows
TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow
However, when rendered to the browser it shows as
<a id="TestBasket_RadGrid1_ctl01_ctl09_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl09$btnUpdateRow','')">
Clicking the button - itemcommand is NOT fired.
On returning from postback the brower shows
<a id="TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl04$btnUpdateRow','')">
Clicking the button - itemcommand is fired.
Anyone explain why clientid gets changed.
I have tried placing a placeholder and creating the control with an id in itemcreated - still same issue.
Thanks in advance for any help.
Looks like the issue happened because of page inheritance.
I made a simple aspx page not using any of our architecture and it seemed to work fine.
I then bought the code back in and it stopped working.
I’ve managed to fix it by changing the page inheritance of the pages that use Telerik controls.
I am new to ASP.net thing, and i have some question regarding postback.
I have a Senario like this:
1) I have a grid on web with a panel inside.
2) I "Insert" the panel with a Web User Control by calling this
Control ctlControl;
ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
pnlTransaction.Controls.Add(ctlControl);
3)The Web User Control providing two button. One is "update" and one is "reset".
Problem is like here:
What i wanted to achieve is when press the "update" button, it will update something back to my DB? But seem after i press the button "Update" or "Reset". The web user control is gone or missing. For my guest is because of the postback issues? Is that correct?
I tried if(!postback) still its doesn't work.
How am i going to overcome this? I already scratching my head about a day?
Thanks you so much.
Regards
LiangCk:
PS:Sorry for my english level, and please dont hesitate to voice out my error or mistake.
well you can convert any of your data columns to template column and then drag and drop your web user control to it
this will result in something like the following code check where "uc1:webUserControle1" is located in the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
<uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
if you are using AJAX, try add updatepanel on your UCT design page
ASP.NET will not preserve a dynamically added user control between postbacks. This is why it is dissapearing. You will need to add the control each time the page is created. However you will need to add it when the control tree is being initialized and restore the original control ID if you want your events to fire. These links provide a full explanation https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx and http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/
You have to Every Time Reload the user control on Page_Init or
Page_Load. Then you can get the Button Click Event and After tha User
Control will not lost.
private void LoadUserControl(){
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath)) {
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
protected void Page_Load(object sender, EventArgs e) {
LoadUserControl();
}