How to get selected row of detail grid in Master-Detail GridView? - devexpress

I'm having a problem on getting the selected rows values of a detail grid. I have master-detail grid in a popup control and it works fine. I also enabled the enable selection to have checkboxes but when i try to run GetSelectedFieldValues on ClientSideEvents of a button it always returns "0". What do i do wrong, i couldn't find it?
Here is the detail grid:
AutoGenerateColumns="False"
CssFilePath="~/App_Themes/Aqua/{0}/styles.css"
CssPostfix="Aqua"
OnBeforePerformDataSelect="gv_ParameterTempD_BeforePerformDataSelect"
ClientInstanceName="gvC_ParameterTempD">
<dxwgv:GridViewCommandColumn
ShowSelectCheckbox="True"
VisibleIndex="0">
</dxwgv:GridViewCommandColumn>
<dxwgv:GridViewDataTextColumn
Caption="Detay Kodu"
FieldName="PrmDetailCode"
VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn
Caption="Seçim Adı"
FieldName="PrmDetailName"
VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn
Caption="Seçim Adı(Grup)"
FieldName="PrmDetailNameG"
VisibleIndex="3">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn
Caption="Seçim Adı(Stok)"
FieldName="PrmDetailNameS"
VisibleIndex="4">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn
Caption="Grup Seçimi Yapan"
FieldName="PrmGroupSelector"
VisibleIndex="5">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn
Caption="Stok Seçimi Yapan"
FieldName="PrmStokSelector"
VisibleIndex="6">
</dxwgv:GridViewDataTextColumn>
</Columns>
And this is the button:
<ClientSideEvents Click="function(s,e)
{
pcc_Question.Hide();
gvC_ParameterTempD.GetSelectedFieldValues('PrmDetailName;PrmDetailNameG;PrmDetailNameS',ShowCellValue);
}"
/>
</dxe:ASPxButton>
and this is the jsscript:
function ShowCellValue(values) {
var value = condition.GetText();
alert(values.length); // here it returns "0"
if(value != "")
{
var newValue = ' ' + value + values + ' = ';
condition.SetText(newValue);
}
else
{
for(var i = 0; i < values.length; i ++) {
value += values[i];
}
condition.SetText(value);
}
}
I don't know what i do wrong,
Thanks for the help

Am I right in my assumption that the button is residing in the same DetailRowTemplate container? Anyway, it is necessary to access the proper instance of the detail GridView object. To do this, set the grid's ClientInstanceName property to a dynamic value. This should allow you to access the proper grid instance and fetch selected row values. A sample code is available at:
http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=Q90007

I'm setting the DataSource at Runtime but i do not call DataBind method, because it makes BeforePerformDataSelect of the Detail Grid to perform more than one.
This code set the master grids datasource and bind it:
protected void
gv_Answers_CustomCallback(object
sender,
ASPxGridViewCustomCallbackEventArgs e)
{
ConfPrmMTempCollection _ConfPrmMTempCollection = new ConfPrmMTempCollection();
masterKey = e.Parameters;
if (masterKey != "")
{
man.Add(new SqlOperatorEquality("MAND_CONF_PRM_M_TEMP.PARAMETER_M_TEMP_ID", Convert.ToInt32(masterKey)));
gv_Answers.DataSource = gc.LoadCollectionFromCollType(typeof(ConfPrmMTempCollection),man);
gv_Answers.DataBind();
man.Clear();
}
}
And this code is for setting the datasource of the detail grid:
protected void
gv_ParameterTempD_BeforePerformDataSelect(object
sender, EventArgs e)
{
ASPxGridView detailGrid = sender as ASPxGridView;
masterKey = detailGrid.GetMasterRowKeyValue().ToString();
man.Add(new SqlOperatorEquality("MAND_CONF_PRM_D_TEMP.PARAMETER_M_TEMP_ID", Convert.ToInt32(masterKey)));
detailGrid.DataSource = gc.LoadCollectionFromCollType(typeof(ConfPrmDTempCollection),man);
}

I see that you set the master grid's DataSource in the CustomCallback event handler. Try to cache the masterKey value in a Session variable and set the grid's DataSource not only in the CustomCallback event handler but also in the Page_Init method:
protected void Page_Init(object sender, EventArgs e) {
if(Session["masterKey"] == null)
return;
ConfPrmMTempCollection _ConfPrmMTempCollection = new ConfPrmMTempCollection();
masterKey = Session["masterKey"].ToString();
if (masterKey != "")
{
man.Add(new SqlOperatorEquality("MAND_CONF_PRM_M_TEMP.PARAMETER_M_TEMP_ID", Convert.ToInt32(masterKey)));
gv_Answers.DataSource = gc.LoadCollectionFromCollType(typeof(ConfPrmMTempCollection),man);
man.Clear();
}
}
protected void Page_Load(object sender, EventArgs e) {
gv_Answers.DataBind();
}
Does this help?

I have created a sample project based on your description and it works fine. It is available to download from:
http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=Q220495

Related

How to add tooltip for Commandfield ButtonType as image

I have to add tooltip for my gridview edit image button. I coded as Commandfield.
<asp:CommandField ButtonType="Image"
HeaderStyle-HorizontalAlign="center"
EditText="Edit" UpdateText="Edit"
ShowEditButton="True"
ItemStyle-VerticalAlign="Top"
EditImageUrl="~/Images/edit.png"
UpdateImageUrl ="~/Images/Save.png" CancelImageUrl ="~/Images/cancel.png"
ItemStyle-Width="15px" ControlStyle-Width="15px">
</asp:CommandField>
Is it possible to add a tooltip in gridview commandfield button type as image?
Following code works well:
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].ToolTip = "Edit Resource Details";
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState.ToString() == "Alternate, Edit")
{
int i = 0;
foreach (TableCell cell in e.Row.Cells)
{
if (e.Row.Cells.GetCellIndex(cell) == 4)
{
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[0])).ToolTip = "Update Resource Details";
((System.Web.UI.LiteralControl)(e.Row.Cells[4].Controls[1])).Text = " ";
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[2])).ToolTip = "Close Resource Details";
}
i++;
}
}
}
}
catch (Exception _e)
{
}
}
or you can
define the EditText property of your gridview's commandfield (as you did), which will render as alt atribute of the <input type='image' alt='Edit' /> tag:
<asp:CommandField ButtonType="Image" EditText="Edit" etc />
then add a script tag to set the title attribute with the following code:
VB:
Protected Sub gvResourceEditor_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResourceEditor.RowDataBound
Dim strScript as String = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});"
Page.ClientScript.RegisterStartupScript(Me.GetType, "SetEditButtonTitle", strScript, True)
End Sub
CS:
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e) {
string strScript = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});"
Page.ClientScript.RegisterStartupScript(this.GetType(), "SetEditButtonTitle", strScript, true);
}
Of course, you may need to taylor the javascript to your grid. This script will set the title attribute of all input tag with type=image.
I didn't try solutions suggested by Sekaran and Show and took a little different approach as I was not willing to rely on JavaScript; thought it might be worth sharing.
A Show's mentioned in his solution that EditText property of Edit button will be rendered as alt attribute of image; this means it should also be stored as AlternateText of the .NET counterpart.
In my case; I have Edit, Update, Delete and Cancel buttons in my CommandField and I wanted to do same for all buttons.
So the (localized) text is in AlternateText property of buttons; I am looping through each buttons and put this value in ToolTip property.
Here is my code.
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Control ctrl in e.Row.Cells[2].Controls)
{
if (ctrl.GetType().BaseType == typeof(ImageButton))
((ImageButton)ctrl).ToolTip = ((ImageButton)ctrl).AlternateText;
}
}
}
I had hardcoded Cell Id "2" in my code as it was well known.
Every Image Button control is of System.Web.UI.WebControls.DataControlImageButton type which is not accessible. Therefore I had used it's BaseType which should be ImageButton
The RegisterStartupScript part of #show's answer does not neeed to run during RowDataBound. There it will run for each row doing exactly the same thing during the grid databind process. You can drop those two lines into the OnPreRender event for the page and it will have the same effect but only happens once during code execution on the server.

Create Asp.net Reset Button

Is there any ways to create reset button to clear all text in text fields in asp form? When user hits the reset button, all text entered by them will clear and they are enable to enter back text in the area.
As per my knowledge there is no such reset functionality provided by Asp.Net.
We can achieve the reset like this
btnReset.Attributes.Add("onClick", "document.forms[0].reset();return false;");
Or
Like this
<input type='button' id='resetButton' value='Reset' onclick='theForm.reset();return false;'/>
Or OnClientclick of asp.net button use this theForm.reset();return false;
try this create a button with reset and in click event write ClearInputs(Page.Controls); and event will call this method.
protected void Button2_Click(object sender, EventArgs e)
{
ClearInputs(Page.Controls);
}
void ClearInputs(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Text = string.Empty;
ClearInputs(ctrl.Controls);
}
}
In the button click method, set all textbox.text.length values to 0. either do it one by one, which is the simple way, or do it by getting all controls of type textbox on the page, which is tad bit more sophisticated, but could be much less typing, depending on the number of textboxes. Definitely more maintainable.
private void ChangeBtn_Click(object sender, EventArgs e)
{
foreach(Control c in Page.Controls)
{
if (c.Controls.Count > 0)
{
foreach(Control c2 in c.Controls)
{
if (c2.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
myspan.InnerHtml = ((TextBox)c2).Text;
((TextBox)c2).Text = ""; //or ((TextBox)c2).Text.Length = 0;
}
}
}
}
}
http://msdn.microsoft.com/en-us/library/20zys56y(v=vs.90).aspx
Create a Click event to the Button control and use the following codes below:
foreach (Control control in Page.Controls)
{
if (control is TextBox)
{
TextBox txt = (TextBox)control;
txt.Text = "";
}
}
This will save you some time to clear all the textboxes inside the web form.
Use Jquery the easiest way to find any type of control and will not have post back event.
$('input[type=text], textarea')
Use foreach loop for clearing value.
Please note that
btnReset.Attributes.Add("onClick", "document.forms[0].reset();return false;");
will not work in clearing pages that are posted back, i.e. If a text box had a value "Silly me" and has been posted back, this code will reset to the post back value which is "Silly me".
The workaround is to repost the page with cleared values - try the following code (it worked for me)
OnClientClick="document.location.href=document.location.href;"
will reload the page with cleared values...
I have multiple type of inputs in my page (TextBox, DropDownList and CheckBox), so here is how I reset them all
Put an <asp:Panel> that wraps my inputs
Run BtnClear_Click on Clear button click
Loop each inputs and reset text/selection/checked value by types
The codes
Default.aspx
<asp:Panel ID="PanelReport" runat="server">
...
<asp:TextBox ID="fldSpeedoMula" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlPlateNo" runat="server" CssClass="form-control"></asp:DropDownList>
<asp:CheckBox ID="cbCard" runat="server" />
<asp:CheckBox ID="cbCash" runat="server" />
<asp:Button ID="BtnClear" runat="server" Text="Clear" CssClass="button" OnClick="BtnClear_Click"/>
...
</asp:Panel>
Default.aspx.cs
protected void BtnClear_Click(object sender, EventArgs e)
{
// Clear all inputs
foreach (DropDownList ddl in PanelReport.Controls.OfType<DropDownList>())
{
ddl.SelectedIndex = 0;
}
foreach (TextBox fld in PanelReport.Controls.OfType<TextBox>())
{
fld.Text = string.Empty;
}
foreach (CheckBox cb in PanelReport.Controls.OfType<CheckBox>())
{
cb.Checked = false;
}
}

How do I change the image on a <asp:buttonField type="Image" /> in code behind?

I have a <asp:GridView > with a <asp:ButtonField ButtonType="Image"> as one of the columns.
Here's the problem: I have to dynamically change the image of this ButtonField during the gridView_RowDataBound(...) event based on the data found in that particular gridview row.
The real question is, is how to access that particular ButtonField inside the gridView_RowDataBound(...) event so I can change its image in C# code?
I can't use
Image imgCtrl = (Image)args.Row.FindControl("ctrlID");
because the <asp:ButtonField> won't allow an ID to be set (get a parser error when I try to run the webPage). And I can't use
args.Row.Cells[0].Controls[0];
because the zeroth index of the .Controls[0] doesn't exist (I get a boundry overflow error).
There's got to be a simple, slick, easy way to do this!
Quick Example :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell tableCell = e.Row.Cells[3]; // Column 3 in the grid have the Image Button
foreach (var control in tableCell.Controls)
{
if (control.GetType() == typeof(System.Web.UI.WebControls.ImageButton)) ;
{
ImageButton iButton = control as ImageButton;
iButton.ImageUrl = "/Logo.jpg";
}
}
}
}

execute code when select is clicked in GridView

I have a GridView and a select link in the GrdiView, when the item is selected I want it to read the contents to a textbox below the GridView. The only way I can think is by accessing the behind code for the onClick function of the select link. The problem is I am unsure how to do this.
UPDATED jams method:
C# code
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.SelectedIndex = e.NewSelectedIndex;
TextBox1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
}
front code
<asp:GridView ID="GridView1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None">
UPDATE my method:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int i = GridView1.SelectedIndex;
ds.Tables["Comments"].Rows[i]["Comment"] = TextBox1.Text;
}
also tried this but I get an error -> System.IndexOutOfRangeException: There is no row at position -1.
Thanks.
You need to attach OnSelectedIndexChanged event with your gridview
<asp:GridView OnSelectedIndexChanged="GridView1_SelectedIndexChanged" />
Then in code behind you could do:
void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = CustomersGridView.SelectedRow;// you will get the selected row
someLabel.Text = row.Cells[0].Text;
}
Once you attach to the selectedindexchanged event you need to also set up some datakeys so you can retrieve values from your grid. See sample here.
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Determine the index of the selected row.
int index = CustomersGridView.SelectedIndex;
// Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " +
CustomersGridView.DataKeys[index].Value.ToString() + ".";
}
it is better to use oORowCommand event of the grid view and pass the command argument and commandName to the same event as a attribute value. Then i think it will work.

set tool tip for each cell of a devexpress gridview

how to set tool tip for each cell of a devexpress gridview in asp.net & c#.net
First, set the "OnHtmlDataCellPrepared" property of your grid, like this:
<dx:ASPxGridView ID="ASPxGridView1" runat="server"
OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared">
</dx:ASPxGridView>
Then set the "title" attribute of the cells in your codebehind:
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
{
e.Cell.Attributes.Add("title", "Your Tooltip");
}
Same concept as mhughes but in my case the gridView is dynamically created so I had to add the event handler after the fact.
_gridview.HtmlDataCellPrepared += new ASPxGridViewTableDataCellEventHandler(_gridview_HtmlDataCellPrepared);
void _gridview_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
{
if (e.CellValue != null)
e.Cell.Attributes.Add("title", e.CellValue.ToString());
}

Resources